Me and my co-worker have been discussing async procedures.
Example:
ws.GetAllEventsAsync("123", "123");
ws.GetAllEventsCompleted += new awc5ws.events.GetAllEventsCompletedEventHandler(ws_GetAllEventsCompleted);
ws in this case is a web service (asmx).
Now, is it possible that the first line of code, executes and finishes before the compiler reaches the 2nd line, thus the callback function is not executed.
So in this case, would it make more sense to attach the event handler callback before actually executing the Web Method? Like so:
ws.GetAllEventsCompleted += new awc5ws.events.GetAllEventsCompletedEventHandler(ws_GetAllEventsCompleted);
ws.GetAllEventsAsync("123", "123");
I don't know if I'm making much sense here, but I hope you can understand my point. I would appreciate if you could go into detail rather than a yes or no answer.
Yes and yes :)
Your first example is a race condition. If you run it, it might work, or it might not. That's actually worse than not working at all.
Sometimes, the call to ws.GetAllEventsAsync
may even execute synchronously, in which case the bug is evident. When it executes asynchronously, it may still complete before your main thread attaches the event handler and you will miss the event.