C# Invoke 使用 异步委托
如果使用多线程,应该会遇到这样的一个问题,在子线程中想调用主线程中(Form1)控件,提示报错!
可以使用Invoke方法调用。
this.Invoke(new MethodInvoker(() =>
{
this.rTxtLog.AppendText("在线程内调用主线程中控件 " + Environment.NewLine);
}));
异步委托学习,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace 线程与异步委托练习
{
class Program
{
//声明一个委托,
public delegate int AddFunDel(int a, int b);
static void Main(string[] args)
{
Console.WriteLine("主线程{0},已启动...", Thread.CurrentThread.ManagedThreadId);
#region 线程
////ParameterizedThreadStart 有参委托
////ThreadStart 无参委托
//Thread thread = new Thread(new ThreadStart(MyFun));
////设置线程名称 给程序员看的。
//thread.Name = "MyFun";
////设置为后台线程。当主线程被关闭时,后台子线程自动被关闭。
//thread.IsBackground = true;
////更改状态,并没有执行,只是告诉CPU,可以执行了。
//thread.Start();
#endregion #region 有参线程
////Thread thread2 = new Thread(new ParameterizedThreadStart(MyFun2));
//Thread thread2 = new Thread(a => {
// while (true)
// {
// Console.WriteLine("子线程{0},正在执行中。参数值:{1}", Thread.CurrentThread.ManagedThreadId, a);
// Thread.Sleep(1000);
// }
//});
//thread2.Name = "MyFun2";
//thread2.IsBackground = true;
////设置优先级,只是建议告诉CPU处理。
//thread2.Priority = ThreadPriority.Highest;
//thread2.Start(99);
#endregion #region 异步委托
//AddFunDel aDel = new AddFunDel(AddFun);
////通过异步调用这个委托
//IAsyncResult iresult = aDel.BeginInvoke(3, 5, null, null);
////...异步委托,在新建子线程中执行委托中方法,主线程被阻塞状态。
//int num = aDel.EndInvoke(iresult);
//Console.WriteLine("计算结果:{0} , 执行线程{1}", num, Thread.CurrentThread.ManagedThreadId);
#endregion #region 异步委托回调函数
AddFunDel aDel = new AddFunDel(AddFun);
//AsyncCallback
IAsyncResult iresult = aDel.BeginInvoke(, , new AsyncCallback(MyAsyncCallback), );
//主线程不会阻塞
while (!iresult.IsCompleted) //IsCompleted 检测异步操作是否已完成。
{
Console.WriteLine("主线程继续执行中...");
Thread.Sleep();
}
#endregion
Console.ReadKey();
} static void MyFun()
{
while (true)
{
Console.WriteLine("子线程{0},正在执行中。", Thread.CurrentThread.ManagedThreadId);
Thread.Sleep();
}
} static void MyFun2(object a)
{
Console.WriteLine("子线程{0},正在执行中。参数值:{1}", Thread.CurrentThread.ManagedThreadId, a);
} static int AddFun(int a, int b)
{
Console.WriteLine("子线程{0},正在执行中,开始运算a+b", Thread.CurrentThread.ManagedThreadId);
Thread.Sleep();
return a + b;
} //异步委托回调函数
static void MyAsyncCallback(IAsyncResult result)
{
AsyncResult aResult = (AsyncResult)result; //封装类 通过接口 实现多态
AddFunDel addDel = (AddFunDel)aResult.AsyncDelegate; //封装委托
int num = addDel.EndInvoke(result); //获取异步操作结果
Console.WriteLine("计算结果:{0} , 执行线程{1}", num, Thread.CurrentThread.ManagedThreadId);
int i = (int)aResult.AsyncState; //BeginInvoke方法中最后一个参数
Console.WriteLine("BeginInvoke最后一个参数调用:{0}", i);
}
}
}
C# Invoke 使用 异步委托的更多相关文章
- c#线程之异步委托begininvoke、invoke、AsyncWaitHandle.WaitOne 、异步回调
单靠自己看书学总是会走很多弯路,任何人也不列外,有些时候自己遇到的很多问题,其它别人在很久之前也可能遇到过,上网查查可以走很大捷径,对自己的学习有很大帮助,刚开始弄线程这块,一开始只是看书,很多东西都 ...
- C#固定时间执行指定事件(观察者模式+异步委托)
最近有个项目需要每天固定的时间去执行指定的事件,发现网上关于这样的文章比较少,而且比较散.通过学习了几篇文章后终于实现了这个功能,在此也特别感谢这些文章的作者们,这也是我第一次在园子里面发文章,望多指 ...
- 异步委托 多线程实现摇奖器 winform版
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 6.26学习 异步委托回调函数 VS 多线程 VS 并行处理
描述: 我现在是轮询着构建实例,然后这个实例去执行一个方法,但是执行方法需要大约10s时间,全部轮询下来需要很长时间.所以我现在要更改,头给了我两个方法,1多线程 2异步委托回调函数. 异步委托回调函 ...
- .net异步委托
委托Delegate是一个类,定义了方法的类型, 使得可以将方法当做另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大佬使用If-Else(Switch)语句,同时使得程序 ...
- 通俗易懂,C#如何安全、高效地玩转任何种类的内存之Span的脾气秉性(二)。 异步委托 微信小程序支付证书及SSL证书使用 SqlServer无备份下误删数据恢复 把list集合的内容写入到Xml中,通过XmlDocument方式写入Xml文件中 通过XDocument方式把List写入Xml文件
通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的脾气秉性(二). 前言 读完上篇<通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的本质(一).>,相信大家对sp ...
- .NET委托解析(异步委托)
上一篇我们了解到了,委托的基本感念,列举了几个委托的实例,并根据实例来反编译源码查看.NET 委托的内部实现,从浅入深的角度来详细的去解析委托的实质,本文将系上篇继续讨论异步委托的实现以及异步委托的源 ...
- Async异步委托
/// <summary> /// 异步委托 /// </summary> public delegate void AsyncHandler(); public static ...
- 异步委托(APM)使用Func异步操作,处理耗时操作
使用委托进行异步操作,处理一些耗时操作,防止主线程阻塞 使用例子: using System; using System.Collections.Generic; using System.Linq; ...
随机推荐
- [转]成员函数指针与高性能的C++委托
原文(作者:Don Clugston):Member Function Pointers and the Fastest Possible C++ Delegates 译文(作者:周翔): 成员函数指 ...
- HDU - 5301 Buildings
Buildings Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total S ...
- Android 四大组件学习之Service六
上几节.我们学习怎样用StartServer启动一个服务,用bindServer去绑定一个服务.以及服务的生命周期,以及什么是IntentService. 也许有读者会发现,我们BindServer中 ...
- LeetCode532. K-diff Pairs in an Array
Description Given an array of integers and an integer k, you need to find the number of unique k-dif ...
- DJI SDK iOS 开发之二:搭建主要的开发环境
本文想介绍搭建主要的DJI SDK iOS下的开发环境,只是DJI官方已经给出了非常具体的执行其demo的教程.网址例如以下: https://dev.dji.com/cn/guide 我这里总结一下 ...
- Spring读取配置文件的方式总结
一.基于XML配置的方式 1.使用 PropertyPlaceholderConfigurer - 在 applicationContext.xml 中配置: <context:property ...
- Android实现录屏直播(二)需求才是硬道理之产品功能调研
请尊重分享成果,转载请注明出处,本文来自Coder包子哥,原文链接:http://blog.csdn.net/zxccxzzxz/article/details/54254244 前面的Android ...
- java 调用cmd命令
public class Port{ public static void main(String[] args) { Runtime runtime=Runtime.getRuntime(); tr ...
- Struts2 主题和模板
实际本章教程开始之前,让我们看看由http://struts.apache.org给出的几个定义: Term 描述 tag A small piece of code executed from wi ...
- Win7机器上安装Ubuntu 14.0.4
折腾了两天,分享一下经历. 我须要在已经安装了win7的机器上安装Ubuntu 14.0.4 (两者共存),研究下来有例如以下几种方案, 都折腾了一遍.分享一下经验: 方式1: wubi.exe, 把 ...