首页 / 网页编程 / ASP.NET / 在Asp.net中调用异步方法-使用信号量
        
            在Asp.net中调用异步方法-使用信号量2011-02-17有些库可能只提供了异步方法,而ASP.net确是同步的,这次就遇到一个问题:页面显示出来以后才会执行回调函数。而我需要的流程是:在回调函数中执行验证,然后才能呈现页面。Mutex,AutoResetEvent提供了通过信号量来协调线程执行步骤的方法。XmppClientConnection是agsxmppJabber库中的类,调用Open会立即返回客户端(ie)呈现该页面,而不管是否成功,同时会在另一个线程会执行登陆,新建帐户的操作,成功后会触发回调事件,这样一来页面呈现后才会执行回调,不符合我们要的逻辑。我们把调用Open的线程叫做:Jabber线程,把执行登陆的线程叫做:Jabber线程的辅助线程。我最初的想法是使用Moniter,代码:private object objlock=new object();
     public void RegisterJab(string username, string password, string server)
     {
       _connection.Server = server;
       _connection.Username = username;
       _connection.Password = password;
       _connection.Port = 80;
       _connection.UseSSL = false;
       _connection.AutoResolveConnectServer = true;
       _connection.ConnectServer = null;
       _connection.SocketConnectionType = agsXMPP.net.SocketConnectionType.Direct;
       _connection.UseStartTLS = true;
       _connection.RegisterAccount = true;
       Moniter.Enter(objlock);
       _connection.Open();
       Moniter.Wait(objlock);
       _connection.Close();
     }
     private void XmppCon_OnRegistered(object sender)
     {
       IsSuccessfull = true;
       Moniter.Exit(objlock);
     }在执行Moniter.Exit()时会抛出异常:SynchronizationLockException,因为Jabber辅助线程并不是锁的拥有者.发现Moniter很像临界区,并不适处理这种情况合。后来,转到了Mutex,Mutex: 是同步基元,它只向一个线程授予对共享资源的独占访问权。如果一个线程获取了互斥体,则要获取该互斥体的第二个线程将被挂起,直到第一个线程释放该互斥体。Mutex很合适这个功能的实现,可是还有没有更简便的方法呢?那就是AutoResetEvent:允许线程通过发信号互相通信。通常,此通信涉及线程需要独占访问的资源。最重要的是他提供了线程间通讯的方法,这样可以更灵活的控制线程的调用步骤,我们用到的就是信号量。代码:namespace LoginBase
{
   public class Register
   {
     XmppClientConnection _connection;
     static AutoResetEvent myResetEvent;
     public bool IsUsed;
     public Register()
     {
       _connection = new XmppClientConnection();
       _connection.SocketConnectionType = agsXMPP.net.SocketConnectionType.Direct;
       _connection.OnLogin += new ObjectHandler(XmppCon_OnLogin);
       _connection.OnRegisterError += new OnXmppErrorHandler(XmppCon_OnRegErr);
       _connection.OnRegistered += new ObjectHandler(XmppCon_OnRegistered);
     }
     public bool IsSuccessfull = false;
     public void RegisterJab(string username, string password, string server)
     {
       _connection.Server = server;
       _connection.Username = username;
       _connection.Password = password;
       _connection.Port = 80;
       _connection.UseSSL = false;
       _connection.AutoResolveConnectServer = true;
       _connection.ConnectServer = null;
       _connection.SocketConnectionType = agsXMPP.net.SocketConnectionType.Direct;
       _connection.UseStartTLS = true;
       _connection.RegisterAccount = true;
       myResetEvent = new AutoResetEvent(false);
       _connection.Open();
       myResetEvent.WaitOne(20 * 1000, true);
       _connection.Close();
     }
     private void XmppCon_OnRegistered(object sender)
     {
       IsSuccessfull = true;
       myResetEvent.Set();
     }
     private void XmppCon_OnLogin(object sender)
     {
       IsSuccessfull = true;
       myResetEvent.Set();
     }
     private void XmppCon_OnRegErr(object sender, Element e)
     {
       //errCode如果是409则已经存在用户
       IsSuccessfull = false;
       Element xn = e.SelectSingleElement("error");
       if (xn.Attribute("code") == "409")
         IsUsed = true;
       myResetEvent.Set();
     }
   }
}先设置为非终止状态,然后进入Jabber线程,阻塞Asp线程,并且等待,超时时间为20秒。如果触发了回调事件,则设置状态为终止,asp线程继续执行。成功完成同步,这样一来,必须等到Jabber辅助线程执行完,Asp线程才会继续下去。