C# 多线程操作之异步委托

在应届生找工作的时候,多线程操作几乎是所有的公司都会问及的一个基本问题。
这里做了一个多线程操作的总结,这里总结了通过异步委托来实现多线程操作。
定义一个委托,是创建一个线程的最简单的方法,并且异步调用它。委托是方法的类型安全的引用。同时委托还智齿异步调用方法。
委托使用线程池来完成异步任务。
当自己的程序使用异步委托的时候,委托会自动创建ige执行线程的任务。委托使用线程池完成异步任务,所有的异步委托调用,都会通过调用系统线程池中的线程来完成调用异步任务。
在下面的简单例子中,我们定义了一个异步委托,并在每次输出的时候显示该函数运行在哪个线程中。
在异步委托中,可以使用三种技术来异步的调用委托。下面分别介绍三种调用异步委托的方法。
1. 投票判断异步委托是否完成
在委托中调用BeginInvoke()方法,返回IAsyncResult结果。程序的源代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace AsyncDelegate
- {
- class Program
- {
- public delegate int TakeSomeTimeDelegate(int data,int ms);
- static void Main(string[] args)
- {
- TakeSomeTimeDelegate dl=TakeSomeTime;
- IAsyncResult ar=dl.BeginInvoke(1,200,null,null);
- while (!ar.IsCompleted)
- {
- Console.WriteLine(".");
- Console.WriteLine("Run in thread:" + Thread.CurrentThread.ManagedThreadId);
- Thread.Sleep(50);
- }
- int result = dl.EndInvoke(ar);
- Console.WriteLine("Result:{0}", result);
- }
- static int TakeSomeTime(int data, int ms)
- {
- Console.WriteLine("TakeSomeTime started!");
- Console.WriteLine("Run in thread:"+Thread.CurrentThread.ManagedThreadId);
- Thread.Sleep(ms);
- Console.WriteLine("TakeSomeTime Completed!");
- return ++data;
- }
- }
- }
该程序的输出结果如图:
可以看到主线程和异步委托线程是同时执行的。
如果在委托结束之前不等待委托完成其他任务就结束主线程,委托线程就会停止。
int result = dl.EndInvoke(ar); 这里的EndInvoke()函数会一直等在异步委托完成并在异步委托完成之前阻断主线程。这样就可以通过这个函数保证异步委托能够正确完成。
2. 等待句柄判断异步委托完成
通过AsyncWatiHandle属性,访问等待句柄。WaitOne()方法阻断当前线程,直到异步调用线程完成返回可以利用的句柄以后再执行当前线程。
程序:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace AsyncDelegate
- {
- class Program
- {
- public delegate int TakeSomeTimeDelegate(int data,int ms);
- static void Main(string[] args)
- {
- TakeSomeTimeDelegate dl=TakeSomeTime;
- IAsyncResult ar=dl.BeginInvoke(1,200,null,null);
- while (true)
- {
- Console.WriteLine(".");
- Console.WriteLine("Run in thread:" + Thread.CurrentThread.ManagedThreadId);
- if (ar.AsyncWaitHandle.WaitOne(100, false))
- {
- Console.WriteLine("Can get the result now");
- break;
- }
- }
- //while (!ar.IsCompleted)
- //{
- // Console.WriteLine(".");
- // Console.WriteLine("Run in thread:" + Thread.CurrentThread.ManagedThreadId);
- // Thread.Sleep(50);
- //}
- int result = dl.EndInvoke(ar);
- Console.WriteLine("Result:{0}", result);
- }
- static int TakeSomeTime(int data, int ms)
- {
- Console.WriteLine("TakeSomeTime started!");
- Console.WriteLine("Run in thread:"+Thread.CurrentThread.ManagedThreadId);
- Thread.Sleep(ms);
- Console.WriteLine("TakeSomeTime Completed!");
- return ++data;
- }
- }
- }
运行结果:
ar.AsyncWaitHandle.WaitOne()阻断了当前线程, 直到异步调用线程完成获得可以利用的句柄以后再次执行当前线程。
3. 利用异步回调函数判断异步调用线程是否结束
回调函数的操作比较复杂, 而且对于程序的理解和维护造成非常大的困难。所以一般情况下能不用回调函数就不要使用回调函数。
使用回调函数,必须注意这个函数从委托线程中调用,而不是从主线程中调用回调函数。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace AsyncDelegate
- {
- class Program
- {
- public delegate int TakeSomeTimeDelegate(int data,int ms);
- static void Main(string[] args)
- {
- TakeSomeTimeDelegate dl=TakeSomeTime;
- IAsyncResult ar = dl.BeginInvoke(1, 200, TakeSomeTimeCompleted, dl);
- for (int i = 0; i < 10;i++ )
- {
- Console.WriteLine(".");
- Console.WriteLine("Run in thread:" + Thread.CurrentThread.ManagedThreadId);
- Thread.Sleep(50);
- }
- //int result = dl.EndInvoke(ar);
- //Console.WriteLine("Result:{0}", result);
- }
- static int TakeSomeTime(int data, int ms)
- {
- Console.WriteLine("TakeSomeTime started!");
- Console.WriteLine("Run in thread:"+Thread.CurrentThread.ManagedThreadId);
- Thread.Sleep(ms);
- Console.WriteLine("TakeSomeTime Completed!");
- return ++data;
- }
- static void TakeSomeTimeCompleted(IAsyncResult ar)
- {
- if (ar==null)
- {
- throw new ArgumentNullException("ar");
- }
- TakeSomeTimeDelegate dl = ar.AsyncState as TakeSomeTimeDelegate;
- int result = dl.EndInvoke(ar);
- Console.WriteLine("result : {0}", result);
- //Console.WriteLine("Run in thread:" + Thread.CurrentThread.ManagedThreadId);
- }
- }
- }
运行结果:
C# 多线程操作之异步委托的更多相关文章
- 6.26学习 异步委托回调函数 VS 多线程 VS 并行处理
描述: 我现在是轮询着构建实例,然后这个实例去执行一个方法,但是执行方法需要大约10s时间,全部轮询下来需要很长时间.所以我现在要更改,头给了我两个方法,1多线程 2异步委托回调函数. 异步委托回调函 ...
- 异步委托(APM)使用Func异步操作,处理耗时操作
使用委托进行异步操作,处理一些耗时操作,防止主线程阻塞 使用例子: using System; using System.Collections.Generic; using System.Linq; ...
- C#实现异步编程的两个简单机制(异步委托&定时器)及Thread实现多线程
创建线程的常用方法:异步委托.定时器.Thread类 理解程序.进程.线程三者之间的区别:简而言之,一个程序至少有一个进程,一个进程至少有一个线程进程就是在内存中运行的程序(即运行着的程序):一个进程 ...
- 异步委托 多线程实现摇奖器 winform版
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- c# 委托访问listbox多线程操作
c# 委托访问listbox多线程操作 using System;using System.Collections.Generic;using System.ComponentModel;using ...
- C#中级-常用多线程操作(持续更新)
一.前言 多线程操作一直是编程的常用操作,掌握好基本的操作可以让程序运行的更加有效.本文不求大而全,只是将我自己工作中常常用到的多线程操作做个分类和总结.平时记性不好的时候还能看看.本文参 ...
- C#多线程操作界面控件的解决方案(转)
C#中利用委托实现多线程跨线程操作 - 张小鱼 2010-10-22 08:38 在使用VS2005的时候,如果你从非创建这个控件的线程中访问这个控件或者操作这个控件的话就会抛出这个异常.这是微软为了 ...
- 通俗易懂,C#如何安全、高效地玩转任何种类的内存之Span的脾气秉性(二)。 异步委托 微信小程序支付证书及SSL证书使用 SqlServer无备份下误删数据恢复 把list集合的内容写入到Xml中,通过XmlDocument方式写入Xml文件中 通过XDocument方式把List写入Xml文件
通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的脾气秉性(二). 前言 读完上篇<通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的本质(一).>,相信大家对sp ...
- c# Winform 多线程操作
主要是对一个过程需要的时间很长执行时会出现界面假死的情况 方法1: Application.DoEvents(),这种方法当你拖动窗体时,界面不会假死.但在你拖动时代码不再执行,也就是阻塞了,当你不再 ...
随机推荐
- 六. Default arguments 参数默认值
示例: 注意点:函数是会默认声明参数变量的,所以不需要再重新声明一次,否则会报错 错误示例如下: 函数参数的传值方法: 需要注意的是:如果要给第二个参数传值,那第一个参数要传undefined,而不能 ...
- 2016.10.4初中部下午NOIP普及组比赛总结
2016.10.4初中部下午NOIP普及组比赛总结 这次的题有些水,只是第四题有点坑. 题目: 比赛:AC+0+AC+50=250 改题:AC+AC+AC+50=350 一.Bill 满地都是水 题目 ...
- Win10弹出需要管理员权限才能删除文件夹,解决办法
Win键+R(就是开始-运行),弹出的输入框输入gpedit.msc回车. 绿色圈内是正解,设置为已禁用.已禁用.已禁用.记着重启才生效.
- fiddler报错:creation of the root certificate was not successful 证书安装不成功
fiddler提示:creation of the root certificate was not successful 证书安装不成功 首先 找到Tools——>Options 在弹出的菜单 ...
- 笔试中sizeof求字节数的问题
1. ]) { cout<<sizeof(ch)<<endl; //或者sizeof(ch)=?; } 这种情况,数组名作为形参,退化成指针,所以sizeof结果是4(32位编 ...
- Docker系列(十三):Kubernetes Service的负载均衡和网络路由的秘密
Kubernetes Service设计分析 什么是单体程序?所有的模块都在一个进程中 微服务,每一个服务是一个进程的模式 kubernetes中的service其实只是一个概念,是一组相同lable ...
- 2018CCPC吉林赛区 | 部分题解 (HDU6555 HDU6556 HDU6559 HDU6561)
// 杭电上的重现赛:http://acm.hdu.edu.cn/contests/contest_show.php?cid=867 // 杭电6555~6566可交题 A - The Fool 题目 ...
- Ubuntu安装QQ、微信、微信开发者工具、搜狗输入法
wine 待补充……
- C++面向对象高级编程(下)第一周-Geekband
勿在浮沙筑高台 革命尚未成功,同志仍需努力 <h1> Conversion Function</h1> class Fraction { public: Fraction(in ...
- [Ceoi2011]Traffic
#2387. [Ceoi2011]Traffic Online Judge:Bzoj-2387,Luogu-4700 Label:Yy,Tarjan缩点,dfs 题目描述 格丁尼亚的中心位于Kacza ...