c#异步Begininvoke使用随笔
2024-08-29 20:36:30
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace WindowsFormsApplication1
{
public class delegateclass
{
private delegate int Dothread();
static Dothread dothread = new Dothread(work); public static void start()
{
AsyncCallback backCall = new AsyncCallback(backcall); dothread.BeginInvoke(backCall, "我是异步调用的parameter");//第一个参数是调用work方法的参数,第二个是回调函数,第三个是需要传到回调函数里的参数可以是Object类型
}
/// <summary>
///这个是委托调用程序
/// </summary>
private static int work()
{
System.Windows.Forms.MessageBox.Show("我是委托调用程序");
return ; }
/// <summary>
/// 回调函数
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
private static int backcall(int parameter)
{
System.Windows.Forms.MessageBox.Show("这是一个回调函数");
return ; }
private static void backcall(IAsyncResult parameter)
{
int result=dothread.EndInvoke(parameter);
System.Windows.Forms.MessageBox.Show("这是一个回调函数");
System.Windows.Forms.MessageBox.Show(result.ToString());
System.Windows.Forms.MessageBox.Show(parameter.AsyncState.ToString()); }
} }
上面是建立的一个类,因时间不多,命名比较随意。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
delegateclass.start();
}
}
}
最新文章
- 为什么angularjs使用ui-router时要使用html5Mode?
- Packet for query is too large(1767212 >; 1048576)mysql在存储图片时提示图片过大
- 【模式匹配】Aho-Corasick自动机
- Output data in a cursor
- android deep link(深度链接)与自定义协议!
- jQuery EasyUI教程之datagrid应用(一)
- C++ 的全局构造与析构函数
- linux下rm误删除数据库文件的恢复方法
- keil 51警告编译优化
- oracle分组查询
- JQ点击高亮显示
- 【剑指offer】删除字符也出现在一个字符串
- WiFi文件上传框架SGWiFiUpload
- centos7,zabbix3.2通过zabbix_java_gateway监控jmx[java/tomcat]
- 背水一战 Windows 10 (113) - 锁屏: 将 Application 的 Badge 通知和 Tile 通知发送到锁屏, 将 secondary tile 的 Badge 通知和 Tile 通知发送到锁屏
- 【转】最近很火的 Safe Area 到底是什么
- Thinkphp5.1 模板路径报错
- 了解各种不同意义上的new
- HMTL列表详解
- 3D建模软件的选择(UG,Solidworks,ProE)