c#—细说多线程(3)

来源:互联网 发布:淘宝店铺如何快速升钻 编辑:程序博客网 时间:2024/05/16 15:48
4.4 委托类       
使用CLR线程池中的工作者线程,最灵活最常用的方式就是使用委托的异步方法,在此先简单介绍一下委托类。

当定义委托后,.NET就会自动创建一个代表该委托的类,下面可以用反射方式显示委托类的方法成员

class Program    {        delegate void MyDelegate();        static void Main(string[] args)        {            MyDelegate delegate1 = new MyDelegate(AsyncThread);            //显示委托类的几个方法成员                 var methods=delegate1.GetType().GetMethods();            if (methods != null)                foreach (MethodInfo info in methods)                    Console.WriteLine(info.Name);            Console.ReadKey();         }     }

委托类包括以下几个重要方法


public class MyDelegate:MulticastDelegate    {        public MyDelegate(object target, int methodPtr);        //调用委托方法        public virtual void Invoke();        //异步委托        public virtual IAsyncResult BeginInvoke(AsyncCallback callback,object state);        public virtual void EndInvoke(IAsyncResult result);    }
当调用Invoke()方法时,对应此委托的所有方法都会被执行。而BeginInvoke与EndInvoke则支持委托方法的异步调用,由BeginInvoke启动的线程都属于CLR线程池中的工作者线程,在下面将详细说明。

4.5 利用BeginInvoke与EndInvoke完成异步委托方法
首先建立一个委托对象,通过IAsyncResult BeginInvoke(string name,AsyncCallback callback,object state) 异步调用委托方法,BeginInvoke 方法除最后的两个参数外,其它参数都是与方法参数相对应的。通过 BeginInvoke 方法将返回一个实现了 System.IAsyncResult 接口的对象,之后就可以利用EndInvoke(IAsyncResult ) 方法就可以结束异步操作,获取委托的运行结果。

class Program    {        delegate string MyDelegate(string name);        static void Main(string[] args)        {            ThreadMessage("Main Thread");                        //建立委托            MyDelegate myDelegate = new MyDelegate(Hello);            //异步调用委托,获取计算结果            IAsyncResult result=myDelegate.BeginInvoke("Leslie", null, null);            //完成主线程其他工作            .............             //等待异步方法完成,调用EndInvoke(IAsyncResult)获取运行结果            string data=myDelegate.EndInvoke(result);            Console.WriteLine(data);                        Console.ReadKey();        }        static string Hello(string name)        {            ThreadMessage("Async Thread");            Thread.Sleep(2000);            //虚拟异步工作            return "Hello " + name;        }        //显示当前线程        static void ThreadMessage(string data)        {            string message = string.Format("{0}\n  ThreadId is:{1}",                   data,Thread.CurrentThread.ManagedThreadId);            Console.WriteLine(message);        }    }

4.6 善用IAsyncResult
在以上例子中可以看见,如果在使用myDelegate.BeginInvoke后立即调用myDelegate.EndInvoke,那在异步线程未完成工作以前主线程将处于阻塞状态,等到异步线程结束获取计算结果后,主线程才能继续工作,这明显无法展示出多线程的优势。此时可以好好利用IAsyncResult 提高主线程的工作性能,IAsyncResult有以下成员:
public interface IAsyncResult{    object AsyncState {get;}            //获取用户定义的对象,它限定或包含关于异步操作的信息。    WailHandle AsyncWaitHandle {get;}   //获取用于等待异步操作完成的 WaitHandle。    bool CompletedSynchronously {get;}  //获取异步操作是否同步完成的指示。    bool IsCompleted {get;}             //获取异步操作是否已完成的指示。}
通过轮询方式,使用IsCompleted属性判断异步操作是否完成,这样在异步操作未完成前就可以让主线程执行另外的工作。
class Program    {        delegate string MyDelegate(string name);        static void Main(string[] args)        {            ThreadMessage("Main Thread");                        //建立委托            MyDelegate myDelegate = new MyDelegate(Hello);            //异步调用委托,获取计算结果            IAsyncResult result=myDelegate.BeginInvoke("Leslie", null, null);            //在异步线程未完成前执行其他工作            while (!result.IsCompleted)            {                Thread.Sleep(200);      //虚拟操作                Console.WriteLine("Main thead do work!");            }            string data=myDelegate.EndInvoke(result);            Console.WriteLine(data);                        Console.ReadKey();        }        static string Hello(string name)        {            ThreadMessage("Async Thread");            Thread.Sleep(2000);            return "Hello " + name;        }        static void ThreadMessage(string data)        {            string message = string.Format("{0}\n  ThreadId is:{1}",                   data,Thread.CurrentThread.ManagedThreadId);            Console.WriteLine(message);        }    }
运行结果:


除此以外,也可以使用WailHandle完成同样的工作,WaitHandle里面包含有一个方法WaitOne(int timeout),它可以判断委托是否完成工作,在工作未完成前主线程可以继续其他工作。运行下面代码可得到与使用 IAsyncResult.IsCompleted 同样的结果,而且更简单方便 。

namespace Test{    class Program    {        delegate string MyDelegate(string name);        static void Main(string[] args)        {            ThreadMessage("Main Thread");                        //建立委托            MyDelegate myDelegate = new MyDelegate(Hello);             //异步调用委托,获取计算结果            IAsyncResult result=myDelegate.BeginInvoke("Leslie", null, null);                        while (!result.AsyncWaitHandle.WaitOne(200))            {                Console.WriteLine("Main thead do work!");            }            string data=myDelegate.EndInvoke(result);            Console.WriteLine(data);                        Console.ReadKey();        }        static string Hello(string name)        {            ThreadMessage("Async Thread");            Thread.Sleep(2000);            return "Hello " + name;        }        static void ThreadMessage(string data)        {            string message = string.Format("{0}\n  ThreadId is:{1}",                   data,Thread.CurrentThread.ManagedThreadId);            Console.WriteLine(message);        }    }
当要监视多个运行对象的时候,使用IAsyncResult.WaitHandle.WaitOne可就派不上用场了。
幸好.NET为WaitHandle准备了另外两个静态方法:WaitAny(waitHandle[], int)与WaitAll (waitHandle[] , int)。
其中WaitAll在等待所有waitHandle完成后再返回一个bool值。
而WaitAny是等待其中一个waitHandle完成后就返回一个int,这个int是代表已完成waitHandle在waitHandle[]中的数组索引。
下面就是使用WaitAll的例子,运行结果与使用 IAsyncResult.IsCompleted 相同。
class Program    {        delegate string MyDelegate(string name);        static void Main(string[] args)        {            ThreadMessage("Main Thread");                        //建立委托            MyDelegate myDelegate = new MyDelegate(Hello);             //异步调用委托,获取计算结果            IAsyncResult result=myDelegate.BeginInvoke("Leslie", null, null);            //此处可加入多个检测对象            WaitHandle[] waitHandleList = new WaitHandle[] { result.AsyncWaitHandle,........ };            while (!WaitHandle.WaitAll(waitHandleList,200))            {                Console.WriteLine("Main thead do work!");            }            string data=myDelegate.EndInvoke(result);            Console.WriteLine(data);                        Console.ReadKey();        }        static string Hello(string name)        {            ThreadMessage("Async Thread");            Thread.Sleep(2000);            return "Hello " + name;        }        static void ThreadMessage(string data)        {            string message = string.Format("{0}\n  ThreadId is:{1}",                   data,Thread.CurrentThread.ManagedThreadId);            Console.WriteLine(message);        }    }

4.7 回调函数
使用轮询方式来检测异步方法的状态非常麻烦,而且效率不高,有见及此,.NET为 IAsyncResult BeginInvoke(AsyncCallback , object)准备了一个回调函数。使用 AsyncCallback 就可以绑定一个方法作为回调函数,回调函数必须是带参数 IAsyncResult 且无返回值的方法: void AsycnCallbackMethod(IAsyncResult result) 。在BeginInvoke方法完成后,系统就会调用AsyncCallback所绑定的回调函数,最后回调函数中调用 XXX EndInvoke(IAsyncResult result) 就可以结束异步方法,它的返回值类型与委托的返回值一致。

class Program    {        delegate string MyDelegate(string name);        static void Main(string[] args)        {            ThreadMessage("Main Thread");            //建立委托            MyDelegate myDelegate = new MyDelegate(Hello);            //异步调用委托,获取计算结果            myDelegate.BeginInvoke("Leslie", new AsyncCallback(Completed), null);            //在启动异步线程后,主线程可以继续工作而不需要等待            for (int n = 0; n < 6; n++)                Console.WriteLine("  Main thread do work!");            Console.WriteLine("");            Console.ReadKey();        }        static string Hello(string name)        {            ThreadMessage("Async Thread");            Thread.Sleep(2000);             \\模拟异步操作            return "\nHello " + name;        }        static void Completed(IAsyncResult result)        {            ThreadMessage("Async Completed");            //获取委托对象,调用EndInvoke方法获取运行结果            AsyncResult _result = (AsyncResult)result;            MyDelegate myDelegate = (MyDelegate)_result.AsyncDelegate;            string data = myDelegate.EndInvoke(_result);            Console.WriteLine(data);        }        static void ThreadMessage(string data)        {            string message = string.Format("{0}\n  ThreadId is:{1}",                   data, Thread.CurrentThread.ManagedThreadId);            Console.WriteLine(message);        }    }
可以看到,主线在调用BeginInvoke方法可以继续执行其他命令,而无需再等待了,这无疑比使用轮询方式判断异步方法是否完成更有优势。
在异步方法执行完成后将会调用AsyncCallback所绑定的回调函数,注意一点,回调函数依然是在异步线程中执行,这样就不会影响主线程的运行,这也使用回调函数最值得青昧的地方。
在回调函数中有一个既定的参数IAsyncResult,把IAsyncResult强制转换为AsyncResult后,就可以通过 AsyncResult.AsyncDelegate 获取原委托,再使用EndInvoke方法获取计算结果。
运行结果如下:

如果想为回调函数传送一些外部信息,就可以利用BeginInvoke(AsyncCallback,object)的最后一个参数object,它允许外部向回调函数输入任何类型的参数。只需要在回调函数中利用 AsyncResult.AsyncState 就可以获取object对象。

class Program    {        public class Person        {            public string Name;            public int Age;        }        delegate string MyDelegate(string name);        static void Main(string[] args)        {            ThreadMessage("Main Thread");            //建立委托            MyDelegate myDelegate = new MyDelegate(Hello);                        //建立Person对象            Person person = new Person();            person.Name = "Elva";            person.Age = 27;                        //异步调用委托,输入参数对象person, 获取计算结果            myDelegate.BeginInvoke("Leslie", new AsyncCallback(Completed), person);                                  //在启动异步线程后,主线程可以继续工作而不需要等待            for (int n = 0; n < 6; n++)                Console.WriteLine("  Main thread do work!");            Console.WriteLine("");            Console.ReadKey();        }        static string Hello(string name)        {            ThreadMessage("Async Thread");            Thread.Sleep(2000);            return "\nHello " + name;        }        static void Completed(IAsyncResult result)        {            ThreadMessage("Async Completed");            //获取委托对象,调用EndInvoke方法获取运行结果            AsyncResult _result = (AsyncResult)result;            MyDelegate myDelegate = (MyDelegate)_result.AsyncDelegate;            string data = myDelegate.EndInvoke(_result);            //获取Person对象            Person person = (Person)result.AsyncState;            string message = person.Name + "'s age is " + person.Age.ToString();            Console.WriteLine(data+"\n"+message);        }        static void ThreadMessage(string data)        {            string message = string.Format("{0}\n  ThreadId is:{1}",                   data, Thread.CurrentThread.ManagedThreadId);            Console.WriteLine(message);        }    }
运行结果:





0 0