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(),这种方法当你拖动窗体时,界面不会假死.但在你拖动时代码不再执行,也就是阻塞了,当你不再 ...
随机推荐
- [symonfy] An error occurred when executing the "'cache:clear --no-warmup'"
Symfony Version: 3.4.* 当运行 composer update 会出现 [RuntimeException] An error occurred when executing t ...
- 【9.14NOIP模拟pj】wtaxi 题解——搜索
[9.14NOIP模拟pj]wtaxi 题目简化 有K辆车,N个人,上车给D元,只有S分钟.上车后无论多少人都要给D元,原地等多少分钟就没了多少元.求最小花费的钱. 我的思路 毫无疑问,此题可以用搜索 ...
- Python-面向对象之封装与多态
目录 组合 什么是组合 使用组合的目的 如何使用组合 封装 什么是封装 为什么要封装 如何封装 访问限制机制 什么是访问限制机制 访问限制机制的目的 如何使用访问限制 property 什么是prop ...
- 软件-版本控制器-VisualSVN:VisualSVN
ylbtech-软件-版本控制器-VisualSVN:VisualSVN VisualSVN 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 0. https:/ ...
- leetcode 854. K-Similar Strings
给定两个字符串, 判断最少经过多少次swap 可以使得 两个字符串一致, 首先类似的问题 是存在一个 underlying graph 的. 每一个字母都对应着一个节点,两个字符串的不一致表示为图上的 ...
- vue页面刷新数据丢失问题
参考: https://blog.csdn.net/aliven1/article/details/80743470 https://blog.csdn.net/liang37712 ...
- webpack 打包生成的index 路径引用不对
webpack 在打包时在访问打包里面的index时,出现路径错误 修改方法为 解决方法:在config下面的index.js把 assetsPublicPath: '/', 修改为: assetsP ...
- [转]WPF的Presenter(ContentPresenter)
这是2年前写了一篇文章 http://www.cnblogs.com/Clingingboy/archive/2008/07/03/wpfcustomcontrolpart-1.html 我们先来看M ...
- Twisted的WEB开发
1 简介 在WEB开发中,偶尔需要对HTTP协议更多底层细节进行控制,这时的django/web.py等等显然无法满足要求,所以只好求助于Twisted了.使用Twisted进行WEB开发,其实更 ...
- IDEA2018激活码
请复制如下内容到文本编辑器(如notepad++)把博客的内容去掉 N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjo ...