1.委托示例(泛型)
2.异步委托示例(泛型)(有参有返回)
3.Thread异步多线程示例(无参无返回)(有参无返回)
4.Task异步多线程示例(有参无返回)
class Program
{
// 定义泛型委托 有参有返回
delegate T MyDelegate<T>(T a, T b, DateTime dt);
static string Tesla(string a, string b, DateTime dt)
{
Console.WriteLine("我是Tesla的 " + a + " 和 " + b + " dt:" + dt);
Thread.Sleep(2000);
return "return:S3XY Tesla a=" + a + " b=" + b + " dt:" + dt;
}
static string BiliBili(string a, string b, DateTime dt)
{
Console.WriteLine("我是BiliBili的 " + a + " 和 " + b + " dt:" + dt + " | 线程ID:" + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(5000);
return "return:BiliBili = " + a + " + " + b + " dt:" + dt + " | 线程ID:" + Thread.CurrentThread.ManagedThreadId;
}
static void AcFun(object obj)
{
Console.WriteLine("我是有参无返回AcFun的 " + obj.ToString() + " | 线程ID:" + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(5000);
}
static void Main(string[] args)
{
// 同步泛型委托
Console.WriteLine("-------------------------------同步泛型委托--------------------------------");
// 同步调用方式1
MyDelegate<string> myDelegate_spaceX = ((a, b, dt) =>
{
Console.WriteLine("我是spaceX的 " + a + " 和 " + b + " dt:" + dt);
Thread.Sleep(2000);
return "return:星辰大海 a=" + a + " b=" + b;
});
string str_spaceX = myDelegate_spaceX("猎鹰", "星舰", DateTime.Now);
Console.WriteLine(str_spaceX);
Console.WriteLine();
// 同步调用方式2
MyDelegate<string> myDelegate_Tesla_S = new MyDelegate<string>(Tesla);
string str_Model_S = myDelegate_Tesla_S("Model S", "Model 3", DateTime.Now);
Console.WriteLine(str_Model_S);
Console.WriteLine();
// 同步调用方式3
MyDelegate<string> myDelegate_Tesla_3 = Tesla;
string str_Model_3 = myDelegate_Tesla_3("Model 3", "Model X", DateTime.Now);
Console.WriteLine(str_Model_3);
Console.WriteLine();
// 同步调用方式4
MyDelegate<string> myDelegate_Tesla_X = Tesla;
string str_Model_X = myDelegate_Tesla_X.Invoke("Model X", "Model Y", DateTime.Now);
Console.WriteLine(str_Model_X);
Console.WriteLine();
// 异步多线程(无参无返回) ThreadStart委托
Console.WriteLine("-------------------------------异步多线程(无参无返回) ThreadStart委托--------------------------------");
Thread thread_AcFun1 = new Thread(new ThreadStart(() =>
{
Thread.Sleep(10000);
Console.WriteLine("我是无参无返回AcFun | 线程ID:" + Thread.CurrentThread.ManagedThreadId);
}));
thread_AcFun1.IsBackground = true;
thread_AcFun1.Priority = ThreadPriority.AboveNormal;// 线程优先级0-4 0最小优先级,4最大优先级,默认是2
thread_AcFun1.Start();
thread_AcFun1.Join();//主线程阻塞,等待分支线程运行结束
Console.WriteLine();
// 异步多线程(有参无返回) ParameterizedThreadStart委托
Console.WriteLine("-------------------------------异步多线程(有参无返回) ParameterizedThreadStart委托--------------------------------");
Thread thread_AcFun2 = new Thread(new ParameterizedThreadStart(AcFun));// ParameterizedThreadStart 委托方法的参数一定是object
thread_AcFun2.IsBackground = true;
thread_AcFun2.Start("object 在这里传参数哟");
thread_AcFun2.Join();//主线程阻塞,等待分支线程运行结束
Console.WriteLine();
// 异步多线程泛型委托(有参有返回)
Console.WriteLine("-------------------------------异步多线程泛型委托(有参有返回)--------------------------------");
MyDelegate<string> myDelegate_BiliBili = new MyDelegate<string>(BiliBili);
IAsyncResult iAsyncResult_BiliBili = myDelegate_BiliBili.BeginInvoke("22娘", "33娘", DateTime.Now, new AsyncCallback(BiliBili_AsyncCallback), null);
int i = 0;
while (!iAsyncResult_BiliBili.IsCompleted)
{
Console.WriteLine("冲鸭 " + i + " | 线程ID:" + Thread.CurrentThread.ManagedThreadId);
i++;
Thread.Sleep(1000);
}
//string str_BiliBili = myDelegate_BiliBili.EndInvoke(iAsyncResult_BiliBili);//获取异步方法的返回值(此时是阻止当前线程的,相当于同步)
//Console.WriteLine("return:同步 myDelegate_BiliBili={0}", str_BiliBili);
Console.WriteLine();
// Task异步多线程 等待所有线程运行结束之后再执行主线程(有参无返回)
Console.WriteLine("-------------------------------Task异步多线程 等待所有线程运行结束之后再执行主线程(有参无返回)--------------------------------");
Action<object> action = new Action<object>((object a) =>
{
Console.WriteLine(DateTime.Now + "入参:" + a + " | 我是Action" + " | 线程ID:" + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(10000);
Console.WriteLine(DateTime.Now + "入参:" + a + " | END | 线程ID:" + Thread.CurrentThread.ManagedThreadId);
});
List<Task> tasks = new List<Task>();
tasks.Add(Task.Factory.StartNew(action, "1.传唯一的一个值在这个"));
tasks.Add(Task.Factory.StartNew(action, "2.StartNew方法有15个重载"));
tasks.Add(Task.Factory.StartNew(action, "3.Action只能有一个object参数或者无参,但是不能有返回值"));
tasks.Add(Task.Factory.StartNew(action, "4.看看这5个是不是多线程"));
tasks.Add(Task.Factory.StartNew(action, "5.看看这5个是不是全部完成之后再走主线程"));
Task.WaitAll(tasks.ToArray());// 等待所有线程执行完毕后执行主线程
Console.WriteLine();
Console.WriteLine("故事的最后还是说了 拜");
Console.ReadKey();
}
/// <summary>
/// BiliBili 异步泛型委托
/// </summary>
/// <param name="result">异步委托的结果</param>
static void BiliBili_AsyncCallback(IAsyncResult result)
{
Console.WriteLine("我是BiliBili_AsyncCallback");
AsyncResult async = (AsyncResult)result;
MyDelegate<string> DelegateInstance = (MyDelegate<string>)async.AsyncDelegate;
string str_BiliBili = DelegateInstance.EndInvoke(result);
Console.WriteLine("return:回调函数中 BiliBili_AsyncCallback={0}", str_BiliBili);
}
}