C#中异步使⽤及回调1. ⼀句话理解异步
我叫你去吃饭,叫完你不去,那我就会⼀直等你,直到你和我⼀起去吃饭。这叫同步!
我叫你去吃饭,叫完不管你去不去,我都不会等你,我⾃⼰去吃饭。这叫异步!
2. 异步使⽤
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);            Action<string> action = t =>
{
for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
};
action.BeginInvoke("异步参数",null,null);
Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);            Console.Read();
}
3. 异步回调
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);            Action<string> action = t =>
{
for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
};
AsyncCallback callback = t =>
{
Console.WriteLine("这⾥是回调函数当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
};
action.BeginInvoke("异步参数", callback, null);
Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);            Console.Read();
}
4. 异步回调带参数
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);            Action<string> action = t =>
{
for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
};
AsyncCallback callback = t =>
{
Console.WriteLine(t.AsyncState);
Console.WriteLine("这⾥是回调函数当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
};
action.BeginInvoke("异步参数", callback, "⽆名⼩虾");
Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);            Console.Read();
}
5. 异步等待
第⼀种⽅式:IsCompleted
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);            Action<string> action = t =>
{
for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
};
AsyncCallback callback = t =>
{
Console.WriteLine(t.AsyncState);
Console.WriteLine("这⾥是回调函数当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
};
IAsyncResult asyncResult = action.BeginInvoke("异步参数", callback, "⽆名⼩虾");
while (!asyncResult.IsCompleted)
{
Console.WriteLine("等待中......");
Thread.Sleep(200);
}
Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);            Console.Read();
}
第⼆种⽅式:WaitOne()
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);            Action<string> action = t =>
{
for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
};
AsyncCallback callback = t =>
{
Console.WriteLine(t.AsyncState);
Console.WriteLine("这⾥是回调函数当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
};
IAsyncResult asyncResult = action.BeginInvoke("异步参数", callback, "⽆名⼩虾");
asyncResult.AsyncWaitHandle.WaitOne();
Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);            Console.Read();
}
第三种⽅式:EndInvoke()
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);            Action<string> action = t =>
{
for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
};
AsyncCallback callback = t =>
{
Console.WriteLine(t.AsyncState);
Console.WriteLine("这⾥是回调函数当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
};
IAsyncResult asyncResult = action.BeginInvoke("异步参数", callback, "⽆名⼩虾");
action.EndInvoke(asyncResult);
Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);
Console.Read();
}
6. 异步返回值
主线程获取到返回值
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);
Func<string,string> func = t =>
{
string result = "这是我的返回值";
for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0},返回的参数是 {2}", Thread.CurrentThread.ManagedThreadId,t, result);
return result;
};
IAsyncResult asyncResult = func.BeginInvoke("异步参数", null, null);
string res = func.EndInvoke(asyncResult);
Console.WriteLine("获取到返回值:{0}", res);
Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);
Console.Read();
}
回调获取到返回值
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);
Func<string,string> func = t =>
{writeline函数
string result = "这是我的返回值";
for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0},返回的参数是 {2}", Thread.CurrentThread.ManagedThreadId,t, result);
return result;
};
AsyncCallback asyncCallback = t =>
{
string res = func.EndInvoke(t);
Console.WriteLine("获取到返回值:{0}", res);
};
func.BeginInvoke("异步参数", asyncCallback, null);
Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);
Console.Read();
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。