Beispiel #1
0
        public void ThreadTaskComparison()
        {
            Stopwatch watch = new Stopwatch();
            //64 is upper limit for WaitHandle.WaitAll() method
            int maxWaitHandleWaitAllAllowed = 64;

            ManualResetEventSlim[] mres =
                new ManualResetEventSlim[maxWaitHandleWaitAllAllowed];

            for (int i = 0; i < mres.Length; i++)
            {
                mres[i] = new ManualResetEventSlim(false);
            }

            long threadTime = 0;
            long taskTime   = 0;

            watch.Start();

            //start a new classic Thread and signal the ManualResetEvent when its done
            //so that we can snapshot time taken, and

            for (int i = 0; i < mres.Length; i++)
            {
                int    idx = i;
                Thread t   = new Thread((state) =>
                {
                    for (int j = 0; j < 10; j++)
                    {
                        Console.WriteLine(string.Format("{0}:, outputing {1}",
                                                        state.ToString(), j.ToString()));
                    }

                    // Signal each event
                    mres[idx].Set();
                });

                t.Start(string.Format("Thread{0}", i.ToString()));
            }

            // WaitHandle.WaitAll((from x in mres select x.WaitHandle).ToArray());
            WaitHandle.WaitAll((mres.Select((x) => x.WaitHandle).ToArray()));

            threadTime = watch.ElapsedMilliseconds;
            watch.Reset();

            for (int i = 0; i < mres.Length; i++)
            {
                mres[i].Reset();
            }

            watch.Start();

            for (int i = 0; i < mres.Length; i++)
            {
                int idx = i;

                // Notice the two args passed to StartNew()
                Task task = Task.Factory.StartNew((state) =>
                {
                    for (int j = 0; j < 10; j++)
                    {
                        Console.WriteLine(string.Format("{0}:, outputing {1}",
                                                        state.ToString(), j.ToString()));
                    }
                    mres[idx].Set();
                }, string.Format("Task{0}", i.ToString()));
            }

            WaitHandle.WaitAll((from x in mres select x.WaitHandle).ToArray());

            taskTime = watch.ElapsedMilliseconds;
            Console.WriteLine("Thread Time waited : {0}ms", threadTime);
            Console.WriteLine("Task Time waited : {0}ms", taskTime);

            for (int i = 0; i < mres.Length; i++)
            {
                mres[i].Dispose();
            }

            Console.WriteLine("All done, press Enter to Quit");
            Console.ReadLine();
        }