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();
}
}
}

c#异步Begininvoke使用随笔的更多相关文章

  1. 多线程 异步 beginInvoke EndInvoke 使用

    有许多耗时操作时,还要响应用户操作.这时候就需要用其他线程或者异步来搞.本来是改造公司的日志组件.因为多上了个国外大区的业务到来本系统来.这个系统其他地方都好就是日志,动不动就要死给我们看.有时候寻找 ...

  2. 异步 BeginInvoke

    委托的异步调用异步多线程的三大特点:1.同步方法卡界面,原因是主线程被占用:异步方法不卡界面,原因是计算交给了别的线程,主线程空闲2.同步方法慢,原因是只有一个线程计算:异步方法快,原因是多个线程同事 ...

  3. C#异步编程(二)

    async和await结构 序 前篇博客异步编程系列(一) 已经介绍了何谓异步编程,这篇主要介绍怎么实现异步编程,主要通过C#5.0引入的async/await来实现. BeginInvoke和End ...

  4. Dispatcher中Invoke与BeginInvoke

    [同步]Invoke Application.Current.Dispatcher.Invoke(AutoIncreaseNumber); [异步]BeginInvoke Application.Cu ...

  5. C#中异步调用示例与详解

    using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServi ...

  6. Net线程间通信的异步机制

    线程间通信 我们看下面的图 我们来看线程间通信的原理:线程(Thread B)和线程(Thread A)通信, 首先线程A 必须实现同步上下文对象(Synchronization Context), ...

  7. C# WinForm 跨线程访问控件

    问题出现: 在WinForm 处理多线程访问主线程的控件时候,就会出现如图所示的错误对话框:    解决方案:  方案一:去掉线程访问主线程UI控件的安全检查,使用: Control.CheckFor ...

  8. .NET C#-- 利用BeginInvoke与EndInvoke完成异步委托方法并获取方法执行返回值示例

    //定义委托 delegate string MyDelegate(string name); //定义委托调用函数 public string Hello(string name) { Thread ...

  9. c#线程之异步委托begininvoke、invoke、AsyncWaitHandle.WaitOne 、异步回调

    单靠自己看书学总是会走很多弯路,任何人也不列外,有些时候自己遇到的很多问题,其它别人在很久之前也可能遇到过,上网查查可以走很大捷径,对自己的学习有很大帮助,刚开始弄线程这块,一开始只是看书,很多东西都 ...

随机推荐

  1. oracle sql 语句 示例

    --oracle 用户对象的导入导出 exp devimage/oracle@172.xx.x.xx/TESTDB owner='devimage' file=d:/devimage.dmp log= ...

  2. JS中数组方法小总结

    1.array.concat(item……) 返回:一个新数组 该方法产生一个新数组,它包含一份array的浅复制,并把一个或多个参数item附加在其后.如果参数item是一个数组,那么它的每个元素会 ...

  3. 写一个c程序辨别系统是大端or小端字节序

    字节序有两种表示方法:大端字节序(big ending),小端字节序(little  ending) 看一个unsigned short 数据,它占2个字节,给它赋值0x1234.若采用的大端字节序, ...

  4. [dp]最长单调递增子序列LIS

    https://www.51nod.com/tutorial/course.html#!courseId=12 解题关键: 如果将子序列按照长度由短到长排列,将他们的最大元素放在一起,形成新序列$B\ ...

  5. koa2,koa1框架安装

    koa2版本安装: npm install koa@ -g hello2.js var Koa = require('koa'); var app = new Koa(); app.use(ctx = ...

  6. C++使用RabbitMQ类库做客户端与RabbitMQ Server通讯,生成C++可调用的rabbimq.*.dll的过程

    Step: download the latest rabbitmq-c via: https://github.com/alanxz/rabbitmq-c follow the document, ...

  7. layui table中使用checkbox

    第一步: 1. <div class="layui-form"> <table class="layui-hide" lay-filter=& ...

  8. Learning Python 004 基础的数据类型和变量

    Python 基础的数据类型和变量 数据类型 整数 Python可以处理任意大小的整数,当然包括负整数. Python表示十六进制也用0x前缀. 浮点数 1.23x10^9和12.3x10^8是完全相 ...

  9. Linux网络编程组播测试代码

    Linux网络编程组播测试代码 (转载)   组播客户端代码如下: #include <sys/types.h>#include <sys/socket.h>#include ...

  10. ES Docs-2:Exploring ES cluster

    The REST API Now that we have our node (and cluster) up and running, the next step is to understand ...