Discuz教程网

C#之委托的异步调用构建轻型线程

[复制链接]
authicon smile 发表于 2010-11-24 13:03:26 | 显示全部楼层 |阅读模式
using System;
using System.Threading;
namespace zuo_Company.MyThreadDelegateProject
{
    public delegate int ThreadRun(int count,int ms);
    public class TestMain
    {
        static void Main(string[] args)
        {
            ThreadRun tr = Total;   //初始化委托变量
            //异步调用,第一式:投票
            IAsyncResult iar = tr.BeginInvoke(1,3000,null,null);
            while (!iar.IsCompleted)
            {
                Console.Write("."); //先主线程Main()输出了一个".",然后才启动的委托方法
                Thread.Sleep(50);
            }
            int result = tr.EndInvoke(iar);
            Console.WriteLine("result:{0}",result);
            Console.WriteLine();
            //异步调用,第二式:等待句柄
            IAsyncResult iaa = tr.BeginInvoke(1, 3000, null, null);
            while (true)
            {
                Console.Write(".");
                if (iaa.AsyncWaitHandle.WaitOne(50, false)) //先启动的委托方法tr=>Total();然后才进入主线程Main()输出"."
                {
                    Console.WriteLine("等待句柄。");
                    break;
                }
            }
            //iaa.AsyncWaitHandle.WaitOne(80, false);
            int result_a = tr.EndInvoke(iaa);
            Console.WriteLine("result_a:{0}", result_a);
            Console.WriteLine();
            //异步调用,第三式:异步回调
            tr.BeginInvoke(8, 3000, CallBack, tr);  //交替输出,线程时间片轮换
            for (int i = 0; i < 1000; i++)  //主线程Main()方法
            {
                Console.Write(".");
            }
            Console.Read();
        }
        static int Total(int count, int ms)
        {
            Console.WriteLine("运行线程方法:");
            //Thread.Sleep(ms);
            for (int i = 0; i < 10000; i++)
            {
                Console.Write("*");
            }
            Console.WriteLine("Thread Method is Over!");
            return ++count;
        }
        //异步回调
        static void CallBack(IAsyncResult ar)
        {
            Console.WriteLine(ar.AsyncState.ToString());
            ThreadRun tt = ar.AsyncState as ThreadRun;
            int ret = tt.EndInvoke(ar);
            Console.WriteLine("CallBack result:{0}",ret);
        }
    }
}
----------------------------------------------------------------------------------------------------
using System;
using System.Threading;
public class AsyncDemo {
public string TestMethod(int callDuration, out int threadId) {
  Console.WriteLine("Test method begins.");
  Thread.Sleep(callDuration);
  threadId = AppDomain.GetCurrentThreadId();
  return "MyCallTime was " + callDuration.ToString();
    }
}
public delegate string AsyncDelegate(int callDuration, out int threadId);
public class AsyncMain {
    private static int threadId;
    static void Main(string[] args) {
     AsyncDemo ad = new AsyncDemo();
     AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);
     IAsyncResult ar = dlgt.BeginInvoke(3000,out threadId,CallbackMethod,dlgt );
     Console.WriteLine("Press Enter to close application.");
     Console.ReadLine();
    }
   
    static void CallbackMethod(IAsyncResult ar) {
     AsyncDelegate dlgt = (AsyncDelegate) ar.AsyncState;
     string ret = dlgt.EndInvoke(out threadId, ar);
     Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret);
    }
}



上一篇:C#之多线程(中)戏说“死锁”
下一篇:令人拍案叫绝的C#正则表达式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 05:49

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表