Working with threads
I am using 3d party library. And i have the following code:
Session s = new Session(AuthParam.Login, AuthParam.Password);
s.Connect();
s.Connected += (sender, eventArgs) =>
{
_contactCollection = s.ContactList.Contacts.Select(x =>开发者_StackOverflow new Contact(x.Nickname, x.Uin)).ToList();
};
s.ConnectionError += (sender, eventArgs) =>
{
};
s.Dispose();
s.Connect working in separate thread. So i want stop executing function. Wait while raised events and then continie executing. How can i do it?
Why could you not reorder your code such that the event handlers are added before calling Connect
?
Session s = new Session(AuthParam.Login, AuthParam.Password);
s.Connected += (sender, eventArgs) =>
{
_contactCollection = s.ContactList.Contacts.Select(x => new Contact(x.Nickname, x.Uin)).ToList();
};
s.ConnectionError += (sender, eventArgs) =>
{
};
s.Connect();
This way you are guarenteed to get the raised events caused by the Connect
method since they are wired in before Connect
is called.
There is really no way to prevent Connect
from executing once it is called.1
1I suppose you could execute Connect
on a separate thread and then suspend that thread, but that is fraught with problems; too many to enumerate here.
精彩评论