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

Reply via email to