我最初的想法是使用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线程才会继续下去。
注:RegisterAsyncTask,也可以实现类似功能。好像它和异步页有很强的关联,用它实现这个类就没有了通用性。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-332342/ ,如需转载,请注明出处,否则将追究法律责任。