温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#中异步回调函数演示源码

发布时间:2020-07-04 13:21:02 来源:网络 阅读:328 作者:aolinhuafu 栏目:编程语言

把代码过程经常用的内容做个珍藏,如下代码是关于C#中异步回调函数演示的代码,应该是对大伙有些用途。

static void Main(string[] args)
{
    Func<string,string> showMessage = ShowMessage;
    IAsyncResult result = showMessage.BeginInvoke("测试异步委托", new AsyncCallback(Completed), null);
    while(!result.IsCompleted)
    {
        Console.WriteLine("主线程可以进行其它的操作!");
    }
    Console.ReadLine();

}
static string ShowMessage(string x)
{
    string current = string.Format("当前线程id为{0}",Thread.CurrentThread.ManagedThreadId);
    Thread.Sleep(3000);
    return string.Format("{0},输入为{1}", current, x);

}
static void Completed(IAsyncResult result)
{
    Console.WriteLine("异步完成!");
    AsyncResult _result = (AsyncResult) result;
    Func<string, string> showMessage = (Func<string, string>) _result.AsyncDelegate;
    Console.WriteLine(showMessage.EndInvoke(_result));
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI