C# Invoke 使用 异步委托
2024-08-23 06:21:07
如果使用多线程,应该会遇到这样的一个问题,在子线程中想调用主线程中(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);
}
}
}
最新文章
- Event 8306 5021 5059 5057发布安全令牌时异常
- MS SQL 日常维护管理常用脚本(二)
- ACM 矩形的个数
- 错误";因为数据库正在使用,所以无法获得对数据库的独占访问权";的解决方案
- 安卓dalvik和art区别
- 在C#中internal关键字是什么意思?和protected internal区别
- WebService 用户名密码验证
- Spring Boot 系列教程7-EasyUI-datagrid
- UITabBarController相关之tabBar文字不显示
- C#中DateTime的缺陷与代替品DateTimeOffset
- JSP中动态include和静态include区别
- JavaScript入门经典(第四版)读书笔记
- Linux regulator framework(1) - 概述【转】
- “64位调试操作花费的时间比预期要长";,无法运行调试解决办法
- 转:.NET 面试题汇总(二)
- [JLOI2009]二叉树问题
- windows、linux mysql部署
- 深入浅出SharePoint——使用WinDbg进行调试
- MINIBASE源代码阅读笔记之heapfile
- orcale 之PL/SQL 控制语句
热门文章
- 键值对集合 dict(字典)
- Office Web Apps Server 2013与PDF(一)
- bat脚本批处理打war打包
- RMI 连接超时时间设定
- 新MBP使用git命令时启用xcode的终端log
- poj 2594(可相交的最小路径覆盖)
- urllib -- ProxyHandler处理器(代理设置)
- Codeforces Beta Round #25 (Div. 2)--A. IQ test
- Python 基础爬虫架构
- Android无线测试之—UiAutomator UiDevice API介绍五