异步编程测试:四种模式.

using System;

namespace AsynTest
{
    
public class AsyncDemo {
        
public delegate string AsyncDelegate(int callDuration,out int threadId);

        
public string TestMethod(int callDuration,out int threadId) {
            Console.WriteLine(
"Test method begins.");
            System.Threading.Thread.Sleep(callDuration);
            threadId
=AppDomain.GetCurrentThreadId();
            
return "MyCallTime was "+callDuration.ToString();
        }

    }
    
    
    
class Class1
    {
        
//使用EndInvoke等待导步调用
        public static void test1() {
            
int threadId;
            AsyncDemo ad
=new AsyncDemo();
            AsynTest.AsyncDemo.AsyncDelegate dlgt
=new AsynTest.AsyncDemo.AsyncDelegate(ad.TestMethod);
            IAsyncResult ar
=dlgt.BeginInvoke(3000,out threadId,null,null);
            
            System.Threading.Thread.Sleep(
0);                        
            Console.WriteLine(
"Main thread {0} does some work.",AppDomain.GetCurrentThreadId());
            
string ret=dlgt.EndInvoke(out threadId,ar);
            Console.WriteLine(
"The call exceute on thread {0},with return value \"{1}\".",threadId,ret);
        }

        
//使用 WaitHandle 等待异步调用
        public static void test2() {
            
int threadId;
            AsyncDemo ad
=new AsyncDemo();
            AsynTest.AsyncDemo.AsyncDelegate dlgt
=new AsynTest.AsyncDemo.AsyncDelegate(ad.TestMethod);
            IAsyncResult ar
=dlgt.BeginInvoke(3000,out threadId,null,null);
            
//阻塞当前线程,以使异部线程开始执行,异部线程开始执行后,主线程就可以继续了.
            System.Threading.Thread.Sleep(0);
            Console.WriteLine(
"Main thread {0} does some work.",AppDomain.GetCurrentThreadId());
            
//阻塞当前线程,直接收到信号为止
            ar.AsyncWaitHandle.WaitOne();
            
string ret=dlgt.EndInvoke(out threadId,ar);
            Console.WriteLine(
"The call exceute on thread {0},with return value \"{1}\".",threadId,ret);
        }

        
//使用 轮询 等待异步调用
        public static void test3() {
            
int threadId;
            AsyncDemo ad
=new AsyncDemo();
            AsynTest.AsyncDemo.AsyncDelegate dlgt
=new AsynTest.AsyncDemo.AsyncDelegate(ad.TestMethod);
            IAsyncResult ar
=dlgt.BeginInvoke(3000,out threadId,null,null);
            
//阻塞当前线程,以使异部线程开始执行,异部线程开始执行后,主线程就可以继续了.
            System.Threading.Thread.Sleep(0);
            Console.WriteLine(
"Main thread {0} does some work.",AppDomain.GetCurrentThreadId());
            
while(!ar.IsCompleted) {
                Console.WriteLine(
"Waiting");
                System.Threading.Thread.Sleep(
10);
            }
            
string ret=dlgt.EndInvoke(out threadId,ar);
            Console.WriteLine(
"The call exceute on thread {0},with return value \"{1}\".",threadId,ret);
        }

        
//使用回调
        ////使用回调方式不阻塞主线程,适合不需要等待异步调用结果的情况.
        public static void test4() {
            
int threadId;
            AsyncDemo ad 
= new AsyncDemo();
            AsynTest.AsyncDemo.AsyncDelegate dlgt 
= new AsynTest.AsyncDemo.AsyncDelegate(ad.TestMethod);
            IAsyncResult ar 
= dlgt.BeginInvoke(3000,
                
out threadId, 
                
new AsyncCallback(test4_forCallback),
                dlgt );
            
            Console.WriteLine(
"Press Enter to close application.");
            Console.ReadLine();
        }
        
public static void test4_forCallback(System.IAsyncResult ar) {
            
int threadId;
            AsynTest.AsyncDemo.AsyncDelegate dlgt 
= (AsynTest.AsyncDemo.AsyncDelegate) ar.AsyncState;
            
            
string ret = dlgt.EndInvoke(out threadId, ar);

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

        }

        [STAThread]
        
static void Main(string[] args)
        {
            test1();
            test2();
            test3();
            test4();
        }
    }
}

转载于:https://www.cnblogs.com/fxwdl/archive/2006/09/12/502074.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值