Search:
Namespaces
Discussions
.NET v1.1
Feedback
Asynchronous Delegates always using same worker thread
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.languages.csharp
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...
Coot
I've been running many tests of the Asynchronous Delegate technique
and I find that although BeginInvoke() does queue the method delegate
onto a worker thread, it always does so on the _same_ thread. So if I
call BeginInvoke() three times in a row, the method delegates are
queued to the same thread and the second method doesn't begin until
the first method completes, and the third doesn't begin until the
second completes.
This occurs whether I detect completion through callback,
AsyncWaitHandle, or polling. It occurs no matter whether the delegate
refers to the same instance method or different instance methods.
The bottom line is that I don't get multi-threaded behavior, but only
two-threaded behavior.
I believe this is not the intended behavior. Has anyone else seen
this? Does anyone know why this is happening?
--
Dave Hein
Reply to this message...
Willy Denoyette [MVP] (VIP)
Not sure what you are exactly doing in your method, but this is the normal
behavior when:
1 - the method returns before it consumed it's CPU quota (this without
executing a blocking call) AND
2 - you run this on a single CPU machine.
Willy.
"Coot" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Coot
"Willy Denoyette [MVP]" <
Click here to reveal e-mail address
> wrote in message news:<
Click here to reveal e-mail address
>...
[Original message clipped]
Yes, it is a single CPU machine _BUT_ no the method doesn't return
that quickly.
Here is the method:
public int TestMethod(
TimeSpan
callDuration, out int threadId )
{
threadId =
AppDomain
.GetCurrentThreadId();
int myThreadId = threadId;
int count = 10;
TimeSpan
subDuration =
TimeSpan
.FromTicks( callDuration.Ticks /
count );
Type t = this.GetType();
for (int i = 0; i < count; ++i)
{
Thread
.Sleep( subDuration );
Console
.WriteLine(
"[" + myThreadId.ToString() + "] "
+ t.FullName + ".TestMethod(): " +
DateTime
.Now.TimeOfDay + " "
+ i.ToString() );
}
return threadId;
}
I'm invoking it with call durations of 0.5 to 1.0 seconds.
The problem occurs if I create a delegate for this method and then use
the BeginInvoke() method of the delegate.
Note: I've also created another class that provides a threaded
implementation that calls that same method via a custom
BeginTestMethod() (and returns results through a custom
EndTestMethod()) -- when I create thread in my custom method I can get
a large number of these TestMethods() executing concurrently -- on my
single CPU box.
But using the async delegate and BeginInvoke(), all I can do is queue
multiple invocations, but they all end up executing, in sequence, on
the same worker thread. :-(
--
Dave Hein
[snip]
Reply to this message...
Willy Denoyette [MVP] (VIP)
"Coot" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
using System;
using System.Threading;
namespace Willys
{
class Tester
{
static void Main()
{
Target t = new Target();
t.SpawnEm();
}
}
}
public class Target {
public delegate int LaunchProc(
TimeSpan
ts, out int v);
public int TestMethod(
TimeSpan
callDuration, out int threadId )
{
threadId =
AppDomain
.GetCurrentThreadId();
int myThreadId = threadId;
int count = 10;
TimeSpan
subDuration =
TimeSpan
.FromTicks( callDuration.Ticks /count );
Type t = this.GetType();
for (int i = 0; i < count; ++i)
{
Thread
.Sleep( subDuration );
Console
.WriteLine("[" + myThreadId.ToString() + "] "
+ t.FullName + ".TestMethod(): " +
DateTime
.Now.TimeOfDay + " "
+ i.ToString() );
}
return threadId;
}
public void SpawnEm() {
int count = 2;
IAsyncResult
[] asr = new
IAsyncResult
[count];
int threadId = 0;
LaunchProc pfn = new LaunchProc(this.TestMethod);
for (int t = 0; t< count; t++) {
asr[t] = pfn.BeginInvoke(new
TimeSpan
(20000000),out threadId, null,
null);
}
foreach(
IAsyncResult
ar in asr)
{
pfn.EndInvoke(out threadId, ar);
Console
.WriteLine("Thread [{0}] Done", threadId);
}
}
}
Gives this:
[2856] Target.TestMethod(): 19:03:39.0448656 0
[2856] Target.TestMethod(): 19:03:39.2451536 1
[2856] Target.TestMethod(): 19:03:39.4454416 2
[2976] Target.TestMethod(): 19:03:39.5455856 0
[2856] Target.TestMethod(): 19:03:39.6457296 3
[2976] Target.TestMethod(): 19:03:39.7458736 1
[2856] Target.TestMethod(): 19:03:39.8460176 4
[2976] Target.TestMethod(): 19:03:39.9461616 2
[2856] Target.TestMethod(): 19:03:40.0463056 5
[2976] Target.TestMethod(): 19:03:40.1464496 3
[2856] Target.TestMethod(): 19:03:40.2465936 6
[2976] Target.TestMethod(): 19:03:40.3467376 4
[2856] Target.TestMethod(): 19:03:40.4468816 7
[2976] Target.TestMethod(): 19:03:40.5470256 5
[2856] Target.TestMethod(): 19:03:40.6471696 8
[2976] Target.TestMethod(): 19:03:40.7473136 6
[2856] Target.TestMethod(): 19:03:40.8474576 9
Thread
[2856] Done
[2976] Target.TestMethod(): 19:03:40.9476016 7
[2976] Target.TestMethod(): 19:03:41.1478896 8
[2976] Target.TestMethod(): 19:03:41.3481776 9
Thread
[2976] Done
So you see clearly that two threadpool threads are activated.
Willy.
Reply to this message...
Coot
Willy,
The behavior seems to be related to how quickly the threads execute.
I modified your code, which uses a 2-second thread duration, to make 3
calls instead of two. All three were executed on a different thread.
However, I then changed it to a 0.2-second thread duration. This time
all 3 calls executed in sequence on the same thread.
And finally, I hunted for the sweet spot and found that with a 0.5
thread duration, the first and second calls executed on different
threads, but the 3rd executed on the same thread as the first.
Not sure why there is some delay in scheduling onto a new thread, but
it seems that the first request is executed immediately, the second is
executed after waiting a 2 or 3 tenths of a second, and the 3rd is
executed after waiting 5 tenthds of a second. Since all were
dispatched within microseconds of each other, I don't understand why
the delay in initiating their execution.
Perhaps this is a tunable policy of the thread pool manager???
--
Dave Hein
"Willy Denoyette [MVP]" <
Click here to reveal e-mail address
> wrote in message news:<
Click here to reveal e-mail address
>...
[snip]
[Original message clipped]
Reply to this message...
System.AppDomain
System.Console
System.DateTime
System.IAsyncResult
System.Threading.Thread
System.TimeSpan
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com