博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在.Net中使用异步(一)

Posted on 2008-04-06 17:52  [虫子]  阅读(4353)  评论(20编辑  收藏  举报

在写程序的过程中,我们可能会需要对某些功能实现异步操作,比如记录调用日志等。

提到异步,我们最容易想到的就是多线程:我们可以启动另外一个线程,把一部分工作交给另外一个线程去执行,而当前线程继续去做一些更加急迫的事情。这里的“把一部分工作交给另外一个线程取执行”,是通过将要执行的函数的函数入口地址告诉另外一个线程来实现的,当新的线程有了函数的入口地址,就可以调用该函数。

我们先来看一下怎样使用C#中的Thread类来实现异步。

  • 使用Thread类异步执行一个方法

在C#中,Thread类是常用的用来启动线程的类:

        static void Main(string[] args)
        
{
            Thread thread 
= new Thread(new ThreadStart(myStartingMethod));
            thread.Start();
        }


        
static void myStartingMethod()
        
{
        }

实际上,这里创建的ThreadStart对象,封装的就是方法“myStartingMethod”的入口地址。C#中通过Delegate对象,可以方便的封装函数入口地址。

而Delegate,实际上是用来描述函数定义的,比如上面提到的ThreadStart委托,他的声明如下:

public delegate void ThreadStart();

这句话声明了一个叫做ThreadStart的委托类型,而且该声明表示:ThredStart这个委托类型,只能封装“返回值为void、没有参数”的函数的入口地址。如果我们给ThreadStart类的构造函数传递的方法不符合,则会出错:

 

        static void Main(string[] args)
        
{
            
// 错误 “myStartingMethod”的重载均与委托“System.Threading.ThreadStart”不匹配
            Thread thread = new Thread(new ThreadStart(myStartingMethod));
            thread.Start();
        }


        
static void myStartingMethod(int a)
        
{
        }

实际上,我们在使用多线程时,要异步执行的函数往往会有一些参数。比如记录日志时,我们需要告诉另外一个线程日志的信息。

  • 异步执行一个带参数的方法

    因此,Thread类除了接受ThreadStart委托,还接受另外一个带参数的委托类型ParameterizedThreadStart:

            static void Main(string[] args)
            
    {
                Thread thread 
    = new Thread(new ParameterizedThreadStart(myStartingMethod));
                thread.Start(null);
            }


            
    static void myStartingMethod(object threadData)
            
    {
                
    // do something.
            }

    ParameterizedThreadStart 委托可以用来封装返回值为void、具有一个object类型参数的函数。这样,我们就可以往另外一个函数中传递参数了——只不过,如果要传递多个参数,我们必须将参数封装一下,弄到一个object对象中去。比如下面的例子中,本来我们需要传递两个整数的,但为了符合ParameterizedThreadStart的声明,我们需要改造一下函数:

            static void Main(string[] args)
            
    {
                Thread thread 
    = new Thread(new ParameterizedThreadStart(myStartingMethod));
                MyStartingMethodParameterWarpper param 
    = new MyStartingMethodParameterWarpper();
                param.X 
    = 1;
                param.Y 
    = 2;
                thread.Start(param);
            }


            
    static void myStartingMethod(object threadData)
            
    {
                MyStartingMethodParameterWarpper param 
    = (MyStartingMethodParameterWarpper)threadData;
                
    int value = param.X + param.Y;
                
    // do something
            }


            
    public class MyStartingMethodParameterWarpper
            
    {
                
    public int X;
                
    public int Y;
            }

    ParameterizedThreadStart委托必须与Thread.Start(Object) 方法一起使用——委托只是用来传递函数入口,但函数的参数是通过Thread.Start方法传递的。

    另外需要注意的,从这里我们可以看到,这样的使用方法并不是类型安全的,我们无法保证myStartingMethod方法的参数threadData永远都是MyStartingMethodParameterWarpper 类型,因此我们还需要加上判断;另外这样实际上也加大了程序间的沟通成本:如果有人需要异步执行myStartingMethod方法,那么他就必须知道其参数的实际类型并保证参数传递正确,而这块编译器已经无法通过编译错误的方式通知你了。

  • 怎样获得异步执行的结果?

    至此,我们只解决了传递参数的问题。

    Thread类无法执行一个包含有返回值的函数。我们知道“int a = Math.Sum(1, 2)”是将Sum函数的返回结果复制给了变量a,但如果用了多线程,那么这个线程不知道将这个返回结果复制到哪里,因此接受这样的一个函数是没有意义的。于是产生了另外一个重要的问题:如果我想要知道一步执行的结果,也就是如果我的线程函数具有返回值,我应该怎样做呢?

    解决的方法有很多种。

    顺着刚才解决传递参数的思路,我们可能会想到:如果Thread类接受一个包含有一个object类型的输入参数和一个object类型的输出参数,不就可以了么?嗯,这个思路听起来不错。不过很不幸的是,MS并没有提供这个接口。

    如此看来,我们是没法直接得到异步函数的执行结果了。

    不过没关系,我们可以间接的得到——我们可以在线程函数内,把函数的返回值保存在一个约定好的地方,然后在主线程到那里去取就可以了!

    因此,考虑到object对象是引用类型,我们可以返回值直接放在线程函数的参数中:

     

            static void Main(string[] args)
            
    {
                Thread thread 
    = new Thread(new ParameterizedThreadStart(myStartingMethod));
                MyStartingMethodParameterWarpper param 
    = new MyStartingMethodParameterWarpper();
                param.X 
    = 1;
                param.Y 
    = 2;
                thread.Start(param);
                
    while(thread.ThreadState != ThreadState.Stopped)
                
    {
                    Thread.Sleep(
    10);
                }

                Console.WriteLine(param.Value);
            }


            
    static void myStartingMethod(object threadData)
            
    {
                MyStartingMethodParameterWarpper param 
    = (MyStartingMethodParameterWarpper)threadData;
                param.Value 
    = param.X + param.Y;
            }


            
    public class MyStartingMethodParameterWarpper
            
    {
                
    public int X;
                
    public int Y;
                
    public int Value;
            }

     

    回顾上面的封装函数参数、封装函数返回值的做法,我们的思路实际上是“将线程函数的参数、返回值封装在对象中”。而刚刚我们也提到了,ParameterizedThreadStart 委托和 Thread.Start(Object) 方法重载使得将数据传递给线程过程变得简单,但由于可以将任何对象传递给 Thread.Start(Object),因此这种方法并不是类型安全的。将数据传递给线程过程的一个更可靠的方法是将线程过程和数据字段都放入辅助对象:

     

            static void Main(string[] args)
            
    {
                MyClass obj 
    = new MyClass();
                obj.X 
    = 1;
                obj.Y 
    = 2;
                Thread thread 
    = new Thread(new ThreadStart(obj.myStartingMethod));
                thread.Start();
                
    while(thread.ThreadState != ThreadState.Stopped)
                
    {
                    Thread.Sleep(
    10);
                }

                Console.WriteLine(obj.Value);
            }


            
    public class MyClass
            
    {
                
    public int X;
                
    public int Y;
                
    public int Value;

                
    public void myStartingMethod()
                
    {
                    
    this.Value = this.X + this.Y;
                }

            }

     

  • 怎样知道线程函数已经执行完毕

    刚才在我们获取函数返回值时,都使用了一个While循环来等待线程函数执行完毕。但这种方式可能是不好的——假设我们启动一个线程,这个线程尝试去获得一个打开的数据库链接,而主程序需要在获得该连接后马上得到通知。看下面这段:

            static void Main(string[] args)
            
    {
                MyClass obj 
    = new MyClass();
                Thread thread 
    = new Thread(new ThreadStart(obj.MyStartingMethod));
                thread.Start();

                
    // 
                if(!SomethingDone && thread.ThreadState == ThreadState.Stopped)
                
    {
                    DoSomething();
                }


                
    // 事情1
                
    // .....
                if(!SomethingDone && thread.ThreadState == ThreadState.Stopped)
                
    {
                    DoSomething();
                }


                
    // 事情2
                
    // .....
                if(!SomethingDone && thread.ThreadState == ThreadState.Stopped)
                
    {
                    DoSomething();
                }


                
    // 事情3
                
    // .....
                if(!SomethingDone && thread.ThreadState == ThreadState.Stopped)
                
    {
                    DoSomething();
                }


                
    // 事情4
                
    // .....
                if(!SomethingDone && thread.ThreadState == ThreadState.Stopped)
                
    {
                    DoSomething();
                }


                
    // ......
            }


            
    static bool SomethingDone = false;
            
    static void DoSomething()
            
    {
                SomethingDone 
    = true;
                
    // do something
            }


            
    public class MyClass
            
    {
                
    public OdbcConnection OpenConnection;

                
    public void MyStartingMethod()
                
    {
                    
    this.OpenConnection = new OdbcConnection();
                    
    // do something
                    this.OpenConnection.Open();
                }

            }

    上面的代码,虽然我们在每次执行一个代码段后就判断线程有没有执行完,但实际上仍然不是及时的——仍然无法保证在函数执行完后就第一时间就启动了函数DoSomething,因为每个代码段执行过程中也许消耗了很长时间,而在这段时间内另一个线程早就执行完了。

    这样的主动轮询的方法,实在是比较累,而且及时性也不好。

    那么,Thread类接受了一个函数入口地址,线程在启动后就会去执行这个函数。那么,假设我们给线程多传递一个函数入口地址,叫线程在执行完线程函数之后就马上执行这个函数,那我们岂不是。。。就能第一时间得知函数已经执行完了?想法很好。看我们来改造:

            static void Main(string[] args)
            
    {
                MyClass obj 
    = new MyClass();
                obj.X 
    = 1;
                obj.Y 
    = 2;
                obj.OnMyStartingMethodCompleted 
    = new MyStartingMethodCompleteCallback(WriteResult);
                Thread thread 
    = new Thread(new ThreadStart(obj.MyStartingMethod));
                thread.Start();

                
    // wait for process exit...
            }


            
    static void WriteResult(MyClass sender)
            
    {
                Console.WriteLine(sender.Value);
            }


            
    public delegate void MyStartingMethodCompleteCallback(MyClass sender);

            
    public class MyClass
            
    {
                
    public int X;
                
    public int Y;
                
    public int Value;
                
    public MyStartingMethodCompleteCallback OnMyStartingMethodCompleted;

                
    public void MyStartingMethod()
                
    {
                    
    this.Value = this.X + this.Y;

                    
    // 函数已经执行完了,调用另外一个函数。
                    this.OnMyStartingMethodCompleted(this);
                }

            }

    注意线程方法MyStartingMethod的最后一句,这里实际上就是执行了委托对象OnMyStartingMethodCompleted中所封装的那个函数入。当然为此我们专门定义了一个表示方法MyStartingMethod已经执行完毕的一个委托MyStartingMethodCompleteCallback,他没有返回值,只有一个参数就是方法MyStartingMethod所属的对象。

    当然,这里的通知,我们也可以使用Event来实现。不过event的实现方法偶就不写了,,今天写的好累。剩下的事情,就留给大家自己搞吧。