private static void ExecuteUsingThreadStart()
        {
            Console.WriteLine("Starting to execute using thread start (new way) ");

            WS2SoapClient ws2 = new WS2SoapClient();
            WS3SoapClient ws3 = new WS3SoapClient();

            IAsyncResult result1 = null;
            IAsyncResult result2 = null;

            // Create threads to execute the methods asynchronously
            Thread startOp3 = new Thread(() => result1 = ws3.BeginOP31());
            Thread startOp2 = new Thread(() => result2 = ws2.BeginOP21());

            // Time the threadas
            var stopWatchBoth = System.Diagnostics.Stopwatch.StartNew();

            // Start the threads
            startOp2.Start();
            startOp3.Start();

            // Make this thread wait until both of those threads are complete
            startOp2.Join();
            startOp3.Join();

            stopWatchBoth.Stop();

            // Display execution time of individual calls
            Console.WriteLine((result1.AsyncState as StateObject));
            Console.WriteLine((result2.AsyncState as StateObject));

            // Display time for both calls together
            Console.WriteLine("Asynchronous Execution Time for both is {0}", stopWatchBoth.Elapsed.TotalSeconds);
        }
        private static void ExecuteUsingWaitHandles()
        {
            Console.WriteLine("Starting to execute using wait handles (old way) ");

            WS2SoapClient ws2 = new WS2SoapClient();
            WS3SoapClient ws3 = new WS3SoapClient();

            IAsyncResult result1 = null;
            IAsyncResult result2 = null;

            // Time the threadas
            var stopWatchBoth = System.Diagnostics.Stopwatch.StartNew();

            result1 = ws3.BeginOP31();
            result2 = ws2.BeginOP21();
            WaitHandle[] handles = { result1.AsyncWaitHandle, result2.AsyncWaitHandle };
            WaitHandle.WaitAll(handles);

            stopWatchBoth.Stop();

            // Display execution time of individual calls
            Console.WriteLine((result1.AsyncState as StateObject));
            Console.WriteLine((result2.AsyncState as StateObject));

            // Display time for both calls together
            Console.WriteLine("Asynchronous Execution Time for both is {0}", stopWatchBoth.Elapsed.TotalSeconds);
        }
        static void Main(string[] args)
        {
            WS2SoapClient ws2 = new WS2SoapClient();
            WS3SoapClient ws3 = new WS3SoapClient();

            //capture time
            DateTime now = DateTime.Now;
            //make calls

            IAsyncResult result1 = null;
            IAsyncResult result2 = null;

            Thread startOp3 = new Thread(
                () => {
                result1 = ws3.BeginOP31();
            }
                );

            Thread startOp2 = new Thread(
                () => {
                result2 = ws2.BeginOP21();
            }
                );

            startOp2.Start();
            startOp3.Start();

            startOp2.Join();
            startOp3.Join();


            //calculate time difference
            TimeSpan ts = DateTime.Now.Subtract(now);

            Console.WriteLine((result1.AsyncState as StateObject));
            Console.WriteLine((result2.AsyncState as StateObject));

            Console.WriteLine(
                "Asynchronous Execution Time (h:m:s:ms): " +
                String.Format("{0}:{1}:{2}:{3}",
                              ts.Hours,
                              ts.Minutes,
                              ts.Seconds,
                              ts.Milliseconds
                              )
                );

            Console.ReadKey();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            WS2SoapClient ws2 = new WS2SoapClient();
            WS3SoapClient ws3 = new WS3SoapClient();

            //capture time
            DateTime now = DateTime.Now;
            //make calls

            IAsyncResult result1 = null;
            IAsyncResult result2 = null;

            // Create threads to execute the methods asynchronously
            Thread startOp3 = new Thread(
                () => {
                result1 = ws3.BeginOP31();
            }
                );

            Thread startOp2 = new Thread(
                () => {
                result2 = ws2.BeginOP21();
            }
                );

            // Time the threadas
            var stopWatchBoth = System.Diagnostics.Stopwatch.StartNew();

            // Start the threads
            startOp2.Start();
            startOp3.Start();

            // Make this thread wait until both of those threads are complete
            startOp2.Join();
            startOp3.Join();

            stopWatchBoth.Stop();

            // Display execution time of individual calls
            Console.WriteLine((result1.AsyncState as StateObject));
            Console.WriteLine((result2.AsyncState as StateObject));

            // Display time for both calls together
            Console.WriteLine("Asynchronous Execution Time for both is {0}", stopWatchBoth.Elapsed.TotalSeconds);
            Console.ReadKey();
        }