"Coot" <dh***@acm.or g> wrote in message
news:a3******** *************** ***@posting.goo gle.com...
"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message
news:<eg******* *******@TK2MSFT NGP12.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.Threadin g;
namespace Willys
{
class Tester
{
static void Main()
{
Target t = new Target();
t.SpawnEm();
}
}
}
public class Target {
public delegate int LaunchProc(Time Span ts, out int v);
public int TestMethod( TimeSpan callDuration, out int threadId )
{
threadId = AppDomain.GetCu rrentThreadId() ;
int myThreadId = threadId;
int count = 10;
TimeSpan subDuration = TimeSpan.FromTi cks( callDuration.Ti cks /count );
Type t = this.GetType();
for (int i = 0; i < count; ++i)
{
Thread.Sleep( subDuration );
Console.WriteLi ne("[" + myThreadId.ToSt ring() + "] "
+ t.FullName + ".TestMetho d(): " + DateTime.Now.Ti meOfDay + " "
+ 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(200000 00),out threadId, null,
null);
}
foreach(IAsyncR esult ar in asr)
{
pfn.EndInvoke(o ut threadId, ar);
Console.WriteLi ne("Thread [{0}] Done", threadId);
}
}
}
Gives this:
[2856] Target.TestMeth od(): 19:03:39.044865 6 0
[2856] Target.TestMeth od(): 19:03:39.245153 6 1
[2856] Target.TestMeth od(): 19:03:39.445441 6 2
[2976] Target.TestMeth od(): 19:03:39.545585 6 0
[2856] Target.TestMeth od(): 19:03:39.645729 6 3
[2976] Target.TestMeth od(): 19:03:39.745873 6 1
[2856] Target.TestMeth od(): 19:03:39.846017 6 4
[2976] Target.TestMeth od(): 19:03:39.946161 6 2
[2856] Target.TestMeth od(): 19:03:40.046305 6 5
[2976] Target.TestMeth od(): 19:03:40.146449 6 3
[2856] Target.TestMeth od(): 19:03:40.246593 6 6
[2976] Target.TestMeth od(): 19:03:40.346737 6 4
[2856] Target.TestMeth od(): 19:03:40.446881 6 7
[2976] Target.TestMeth od(): 19:03:40.547025 6 5
[2856] Target.TestMeth od(): 19:03:40.647169 6 8
[2976] Target.TestMeth od(): 19:03:40.747313 6 6
[2856] Target.TestMeth od(): 19:03:40.847457 6 9
Thread [2856] Done
[2976] Target.TestMeth od(): 19:03:40.947601 6 7
[2976] Target.TestMeth od(): 19:03:41.147889 6 8
[2976] Target.TestMeth od(): 19:03:41.348177 6 9
Thread [2976] Done
So you see clearly that two threadpool threads are activated.
Willy.