There are 2 diff I can think of: 1. QueueUserWorkItem, doesn't require EndInvoke to be called in order not to leak (see discussion elsewhere) 2. In the case of remoting Delegate.BeginInvoke will actually use different mechanism (async) and the result will endup in a different part of the threapool (sorry for not finding a better terminalogy). think of CPIO Threads vs. Worker Threads.
Dmitriy
Richard Blewett wrote:
IIRC correctly the ThreadPool class is marginally faster although generally a bit less programmer friendly.
And yes, they both queue work up to the CLR threadpool.
Regards
Richard Blewett DevelopMentor
-----Original Message----- From: Moderated discussion of advanced .NET topics. [mailto:[EMAIL PROTECTED] On Behalf Of Sean Chase MCAD, MCSD Sent: 11 March 2004 02:19 To: [EMAIL PROTECTED] Subject: [ADVANCED-DOTNET] BeginInvoke vs. ThreadPool.QueueUserWorkItem
Am I comparing apples to oranges here? Other than the asynch callback with BeginInvoke, why would I use one over the other. Further, do both ways use threads from the thread pool?
//////////////////////////// ///First Way /////////////////////////// using System; using System.Collections; using System.Threading; using System.Diagnostics;
class App { [STAThread] static void Main(string[] args) { Utility u = new Utility(); ThreadPool.QueueUserWorkItem(new WaitCallback(u.GetPrimes), 100000); Console.Read(); } }
class Utility { public void GetPrimes(object objState) { int max = (int) objState; ArrayList list = new ArrayList(); BitArray flags = new BitArray(max + 1); for (int i = 2; i <= max; i++) { if (!flags[i]) for (int j = i * 2; j <= max; j += i) flags[j] = true; } for(int i = 3; i < flags.Length; i++) if(!flags[i]) list.Add(i);
if(list != null) { foreach(int i in list) { Console.WriteLine(i); } } } }
//////////////////////////// ///Second Way /////////////////////////// using System.Runtime.Remoting.Messaging; delegate ArrayList GetPrimesDelegate(int max);
class App { [STAThread] static void Main(string[] args) { Utility u = new Utility(); GetPrimesDelegate d = new GetPrimesDelegate(u.GetPrimes); d.BeginInvoke(100000, new AsyncCallback(PrimesProcessed),null); Console.Read(); }
static void PrimesProcessed(IAsyncResult ar) { Debug.WriteLine("PrimesProcessed called..."); Debug.Flush(); AsyncResult aResult = (AsyncResult)ar; GetPrimesDelegate temp = (GetPrimesDelegate)aResult.AsyncDelegate; ArrayList list = temp.EndInvoke(ar);
if(list != null) { foreach(int i in list) { Console.WriteLine(i); } } } }
class Utility { public ArrayList GetPrimes(int max) { ArrayList list = new ArrayList(); BitArray flags = new BitArray(max + 1); for (int i = 2; i <= max; i++) { if (!flags[i]) for (int j = i * 2; j <= max; j += i) flags[j] = true; } for(int i = 3; i < flags.Length; i++) if(!flags[i]) list.Add(i); return list; } }
Can someone help me shine some light on this? Thanks!
=================================== This list is hosted by DevelopMentor. http://www.develop.com Some .NET courses you may be interested in:
NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles http://www.develop.com/courses/gaspdotnetls
View archives and manage your subscription(s) at http://discuss.develop.com
--- Incoming mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.614 / Virus Database: 393 - Release Date: 05/03/2004
--- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.614 / Virus Database: 393 - Release Date: 05/03/2004
=================================== This list is hosted by DevelopMentor® http://www.develop.com Some .NET courses you may be interested in:
NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles http://www.develop.com/courses/gaspdotnetls
View archives and manage your subscription(s) at http://discuss.develop.com
=================================== This list is hosted by DevelopMentor® http://www.develop.com Some .NET courses you may be interested in:
NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles http://www.develop.com/courses/gaspdotnetls
View archives and manage your subscription(s) at http://discuss.develop.com
