Weird behaviour with Async method call
-
I'm trying to learn async threading and I stumbled on the first piece of code. It works but with weird results. I'd be glad if you could point out what's wrong with it. Here's the code I'm trying:
private static void LongProcess(int waitTime)
{
Trace.WriteLine("LongProcess started");
Thread.Sleep(waitTime);
Trace.WriteLine("LongProcess finished");
}private delegate void LongProcessDelegate(int waitTime);
private void Start()
{
LongProcessDelegate lp = LongProcess;
IAsyncResult ar = lp.BeginInvoke(2000, null, null);
while(!ar.Completed)
{
Trace.WriteLine("Not finished yet");
Thread.Sleep(100);
}
lp.EndInvoke(ar);
Trace.WriteLine("All completed");
}The problem is with the results. This is what I expected:
LongProcess started
Not finished yet
..
Not finished yet
LongProcess finished
All completedBut here's the Trace log when I set it to 1000ms:
LongProcess finished
Not finished yet
..
Not finished yet
LongProcess finished
All completedAt 2000ms:
Not finished yet
LongProcess finished
Not finished yet
..
Not finished yet
LongProcess finished
All completedWhat's wrong with this? Am I understanding async calls wrong?
-
I'm trying to learn async threading and I stumbled on the first piece of code. It works but with weird results. I'd be glad if you could point out what's wrong with it. Here's the code I'm trying:
private static void LongProcess(int waitTime)
{
Trace.WriteLine("LongProcess started");
Thread.Sleep(waitTime);
Trace.WriteLine("LongProcess finished");
}private delegate void LongProcessDelegate(int waitTime);
private void Start()
{
LongProcessDelegate lp = LongProcess;
IAsyncResult ar = lp.BeginInvoke(2000, null, null);
while(!ar.Completed)
{
Trace.WriteLine("Not finished yet");
Thread.Sleep(100);
}
lp.EndInvoke(ar);
Trace.WriteLine("All completed");
}The problem is with the results. This is what I expected:
LongProcess started
Not finished yet
..
Not finished yet
LongProcess finished
All completedBut here's the Trace log when I set it to 1000ms:
LongProcess finished
Not finished yet
..
Not finished yet
LongProcess finished
All completedAt 2000ms:
Not finished yet
LongProcess finished
Not finished yet
..
Not finished yet
LongProcess finished
All completedWhat's wrong with this? Am I understanding async calls wrong?
kensai wrote:
Am I understanding async calls wrong?
Yes. Obviously you expect the code in the LongProcess to start immediately when you call the BeginInvoke method, but that doesn't happen. A new thread is started to run the method, and you can't predict exactly when that thread starts to run. Unless you have a multi-core processor, the thread running the LongProcess method will rarely start before you call Tread.Sleep in the main thread.
Despite everything, the person most likely to be fooling you next is yourself.
-
kensai wrote:
Am I understanding async calls wrong?
Yes. Obviously you expect the code in the LongProcess to start immediately when you call the BeginInvoke method, but that doesn't happen. A new thread is started to run the method, and you can't predict exactly when that thread starts to run. Unless you have a multi-core processor, the thread running the LongProcess method will rarely start before you call Tread.Sleep in the main thread.
Despite everything, the person most likely to be fooling you next is yourself.