Exemplo n.º 1
0
        public void UseDelegate()
        {
            // The asynchronous method puts the thread id here.
            int threadId;

            // Create an instance of the test class.
            AsyncDemo ad = new AsyncDemo();

            // Create the delegate.
            AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);

            // Initiate the asychronous call.
            IAsyncResult result = caller.BeginInvoke(3000,
                                                     out threadId, null, null);

            Thread.Sleep(200);
            Console.WriteLine("Main thread {0} does some work.",
                              Thread.CurrentThread.ManagedThreadId);

            // Call EndInvoke to wait for the asynchronous call to complete,
            // and to retrieve the results.
            string returnValue = caller.EndInvoke(out threadId, result);

            Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
                              threadId, returnValue);
        }
Exemplo n.º 2
0
        public async Task <Tuple <int, string> > StartAsync(int callDuration)
        {
            Console.WriteLine($"Main thread {0} does some work.",
                              Thread.CurrentThread.ManagedThreadId);
            // Create an instance of the test class.
            AsyncDemo ad    = new AsyncDemo();
            var       tuple = await ad.TestMethodAsync(4000);

            //var tuple1 = await ad.TestMethodAsync(2000);

            Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
                              tuple.Item1, tuple.Item2);
            return(tuple);
        }