好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#使用委托的形式调用线程代码实例

委托

对于委托,我们都知道他是一个引用类型,具有引用类型所具有的通性。需要知道的是它保存的不是实际值,只是是保存对存储在托管堆中的对象的引用。或说的直接点,委托就相当于叫人帮忙,让你帮你做一些事情。我这里就使用委托的形式,调用线程,来简单的说一下。

代码如下:

?

using system;

using system.threading;

namespace _012_线程

{

   class program

   {

     static void main( string [] args) //在mian中线程是执行一个线程里面的语句的执行,是从上到下的

     {

       //通过委托 开启一个线程

       //==============可用泛型传参数(无返回值)==============

       action threaa = threadtesta;

       threaa.begininvoke( null , null ); //开启一个新的线程去执行,threaa所引用的方法

       action< int > threab = threadtestb;

       threab.begininvoke(111, null , null );

       //可以认为线程是同时执行的 (异步执行)

       console.writeline( "异步执行" );

       //================带返回值的形式====================

       //第一种方式 检测线程结束  ----- iscompleted线程是否行完毕

       //func<int, int> threac = threadtestc;

       ////接收异步线程返回值

       //iasyncresult returnresult = threac.begininvoke(111, null, null);

       //while (!res.iscompleted)

       //{

       //  console.write(".");

       //  thread.sleep(10); //控制子线程的检测频率,(每10ms检测一次)

       //}

       ////取得异步线程返回值

       //int result = threac.endinvoke(res);

       //console.writeline("iscompleted方式检测:" + result);

       //第二种方式 检测线程结束  -----  1000ms没结束就返回false,反之

       func< int , int > threac = threadtestc;

       //接收异步线程返回值

       iasyncresult returnresult = threac.begininvoke(111, null , null );

       bool isend = returnresult.asyncwaithandle.waitone(1000);

       int result = 0;

       if (isend)

       {

         result = threac.endinvoke(returnresult);

       }

       console.writeline( "endinvoke()方式检测:" + isend + " " + result);

       //第三种方式  检测线程结束  ----- 通过回调,检测线程结束

       func< int , string , string > thread = threadtestd;

       //倒数第二个参数,表示委托类型的参数,(回调函数)当线程结束的时候会调用这个委托指向的方法

       //最后一个参数,用来给回调函数传递数据

       iasyncresult asy = thread.begininvoke(111, "czhenya" , oncallkey, thread);

       //改为lamdba表达式

       thread.begininvoke(111, "czhenya" ,(ar)=>{

         string res = thread.endinvoke(ar);

         console.writeline( "在lamdba表达式中取得:" +res);

       }, null );

       console.readkey();

     }

     static void oncallkey(iasyncresult ar)

     {

       func< int , string , string > thread = ar.asyncstate as func< int , string , string >;

       string res = thread.endinvoke(ar);

       console.writeline( "在回调函数中取到的结果 :" +res);

     }

     /// <summary>

     /// 一般是比较耗时的操作方法

     /// </summary>

     static void threadtesta()

     {

       console.writeline( "threatesta" );

     }

     static void threadtestb( int num)

     {

       console.writeline( "threatestb " +num);

     }

     static int threadtestc( int num)

     {

       console.writeline( "threatestc" );

       thread.sleep(100); //让当前线程休眠(暂停线程(参数单位:ms))

       return num;

     }

     static string threadtestd( int num, string str)

     {

       console.writeline( "threatestd" );

       return num + " " + str;

     }

   }

}

运行结果图:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/Czhenya/article/details/78225963

dy("nrwz");

查看更多关于C#使用委托的形式调用线程代码实例的详细内容...

  阅读:44次