zoukankan      html  css  js  c++  java
  • 委托异步体验

     1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.Threading;
    6
    7 namespace Simple
    8 {
    9 internal delegate void PrintMsg();
    10
    11 class DelegateAsync
    12 {
    13 public event PrintMsg PrintMsg;
    14
    15 public DelegateAsync()
    16 {
    17 }
    18
    19 public void Execute()
    20 {
    21
    22 for (int i = 0; i < 10; i++)
    23 {
    24 if (PrintMsg!=null)
    25 {
    26 PrintMsg.BeginInvoke(new AsyncCallback(HandlerCallback),null);
    27 Console.WriteLine("Thread" + Thread.CurrentThread.ManagedThreadId.ToString() + " run now>>>>>>>>");
    28 }
    29 //PrintMsg.EndInvoke(ar);
    30 }
    31 Console.WriteLine("works completed!");
    32 }
    33
    34 void HandlerCallback(IAsyncResult ar)
    35 {
    36 PrintMsg pm=ar.AsyncState as PrintMsg;
    37 string id = Thread.CurrentThread.ManagedThreadId.ToString();
    38 Console.WriteLine("Thread" + id + " is " + Thread.CurrentThread.IsThreadPoolThread);
    39 if (pm!=null)
    40 {
    41 pm.EndInvoke(ar);
    42 }
    43 }
    44 }
    45 }
     1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.Threading;
    6
    7 namespace Simple
    8 {
    9 class Program
    10 {
    11 static void Main(string[] args)
    12 {
    13 DelegateAsync da=new DelegateAsync();
    14 da.PrintMsg += new PrintMsg(Da_PrintMsg);
    15 da.Execute();
    16 Console.Read();
    17 }
    18
    19 static void Da_PrintMsg()
    20 {
    21 string id = Thread.CurrentThread.ManagedThreadId.ToString();
    22 Console.WriteLine("Thread" + id + " is " + Thread.CurrentThread.IsThreadPoolThread);
    23 Console.WriteLine("Thread" + id + " work start now!");
    24 Thread.Sleep(10000);
    25 Console.WriteLine("Thread" + id + " am work completed now!");
    26 }
    27 }
    28 }



  • 相关阅读:
    开发常用工具
    eclipse官方下载地址
    spring相关下载地址
    Django在windows系统下安装详情
    Python with用法:自动关闭文件原理
    python字符串使用哪种编码格式?
    python浮点数精度问题
    robotframework 自定义库导入 modulenotfounderror
    git 解决 push 时(master|MERGING)
    git撤销对 远程的push&commit提交
  • 原文地址:https://www.cnblogs.com/kingge/p/2288577.html
Copyright © 2011-2022 走看看