"Coot" <dh***@acm.org> wrote in message
news:a3**************************@posting.google.c om...
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:<eg**************@TK2MSFTNGP12.phx.gbl>... 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.
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]
Well, this code:
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.