码迷,mamicode.com
首页 > 其他好文 > 详细

委托的异步执行_demp

时间:2019-05-12 13:40:36      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:ati   ring   string   lin   task   .com   start   四种   rom   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace async_demo
{
class Program
{
static void Main(string[] args)
{
//委托的异步执行等待结束的四种情况:
//1.endinvoke等待执行结束;
//2.asyncwaithandle 和第一种差不多会堵住当前线程一直在那等待
//3.通过判断.completed查看是否完成,如果没有完成可以执行一些主线程上的信息,不至于死堵着
//4.对于执行完成后能够执行一个回调的方法,直接写到begininovke中去

//4.new asynccallback
test_4();


//3.complted来判断是否完成
// test_3();

//2.AsyncWaitHandle
// test_2();


//1.EndInvoke
// test_1();

Console.ReadKey();

}

private static void test_4()
{
Func<string, string> fc = new Func<string, string>((string str) =>
{
Console.WriteLine(str +"---->start");
System.Threading.Thread.Sleep(2000);
return "result_msg";

});

Console.WriteLine("main start");

var tt= fc.BeginInvoke("input_str", new AsyncCallback((obj)=>{
Func<string, string> fcc = obj.AsyncState as Func<string, string>; //此方法的执行是tt.iscompleted是true的时候执行

Console.WriteLine("jinru___");

System.Threading.Thread.Sleep(500);

Console.WriteLine("进入等待");

string ss= fcc.EndInvoke(obj); //obj指的是 fc的执行返回 IAsyncResult

 

Console.WriteLine(ss +"async completed");

System.Threading.Thread.Sleep(1000);
Console.WriteLine("执行回调函数的内容");


}), fc);

while (!tt.IsCompleted)
{
Console.WriteLine("稍等--正在等代异步是否执行完成");
System.Threading.Thread.Sleep(200);
}
{

}

Console.WriteLine("main_progromm__star");
}

private static void test_3()
{
Func<string, string> fc = new Func<string, string>((string str) =>
{
Console.WriteLine(str + "---------->start");

System.Threading.Thread.Sleep(2000);
return "result_msg";
});

var tt = fc.BeginInvoke("input", null, null);

Console.WriteLine("mail progress");

while (!tt.IsCompleted)
{
Console.WriteLine("wait......." + DateTime.Now.ToString("yyyyMMddHHmmss.fff"));
System.Threading.Thread.Sleep(200);
}

string ss = fc.EndInvoke(tt);

Console.WriteLine(ss + System.Threading.Thread.CurrentThread.ManagedThreadId);

}

private static void test_2()
{
Func<string, string> fc = new Func<string, string>((obj) =>
{
Console.WriteLine(obj);

return "result_msg";
});

var tt = fc.BeginInvoke("input_msg", null, null);
Console.WriteLine("mail progress");

tt.AsyncWaitHandle.WaitOne(3000);

Console.WriteLine("asyncwaithandle.waitone over");

string ss = fc.EndInvoke(tt);

Console.WriteLine(ss + DateTime.Now.ToString("yyyyMMddHHmmss.fff"));

tt.AsyncWaitHandle.Close();

Console.WriteLine("关闭了asyncwaithandle");
}


private static void test_1()
{
Func<string, string> fc = new Func<string, string>((string in_str) =>
{

System.Threading.Thread.Sleep(2000); //模拟业务执行
Console.WriteLine("{0}-->{1}--{2}", in_str, System.Threading.Thread.CurrentThread.ManagedThreadId,
DateTime.Now.ToString("yyyyMMddHHmmss.fff"));

return in_str + "res_result" + DateTime.Now.ToString("yyyyMMddHHmmss.fff");

});

var tt = fc.BeginInvoke("input_msg", null, null);

Console.WriteLine("mail_progress" + "-->{0}--{1}",
System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyyMMddHHmmss.fff"));

string ss = fc.EndInvoke(tt);

Console.WriteLine("返回的信息:" + ss);

Console.WriteLine("mail_progress" + "--->{0}--{1}",
System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyyMMddHHmmss.fff"));

}


}
}

委托的异步执行_demp

标签:ati   ring   string   lin   task   .com   start   四种   rom   

原文地址:https://www.cnblogs.com/muzililong/p/10851952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!