C#在线程池中调用委托
2016-03-02 21:11
489 查看
在线程池中调用线程,同时启用超时等待。
可应场景:发送信号,等待回复;收到回复->回复处理;未收到回复->超时处理
using System;
using System.Threading;
namespace InvokingADelegate
{
class Program
{
static void Main(string[] args)
{
int threadId = 0;
RunOnThreadPool poolDelegate = Test;
var t = new Thread(() => Test(out threadId));
t.Start();
t.Join();
Console.WriteLine("thread id: {0}", threadId);
//回调函数在委托函数调用结束后开始
IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call");
//等待,直到线程结束
r.AsyncWaitHandle.WaitOne();
//while(!r.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(0.1)))
//{
// Console.Write("*");
//}
//获取线程运行结束后的结果
string result = poolDelegate.EndInvoke(out threadId, r);
Console.WriteLine("thread pool worker thread id: {0}", threadId);
Console.WriteLine(result);
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.ReadKey();
}
private delegate string RunOnThreadPool(out int threadId);
private static void Callback(IAsyncResult ar)
{
Console.WriteLine("starting a callback...");
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.WriteLine("starting passed to a callback:{0}", ar.AsyncState);
Console.WriteLine("is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
Console.WriteLine("thread pool worker thread id: {0}", Thread.CurrentThread.ManagedThreadId);
}
private static string Test(out int threadId)
{
Console.WriteLine("starting...");
Console.WriteLine("is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
Thread.Sleep(TimeSpan.FromSeconds(2));
threadId = Thread.CurrentThread.ManagedThreadId;
return string.Format("thread pool worker thread id was:{0}", threadId);
}
}
}
可应场景:发送信号,等待回复;收到回复->回复处理;未收到回复->超时处理
using System;
using System.Threading;
namespace InvokingADelegate
{
class Program
{
static void Main(string[] args)
{
int threadId = 0;
RunOnThreadPool poolDelegate = Test;
var t = new Thread(() => Test(out threadId));
t.Start();
t.Join();
Console.WriteLine("thread id: {0}", threadId);
//回调函数在委托函数调用结束后开始
IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call");
//等待,直到线程结束
r.AsyncWaitHandle.WaitOne();
//while(!r.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(0.1)))
//{
// Console.Write("*");
//}
//获取线程运行结束后的结果
string result = poolDelegate.EndInvoke(out threadId, r);
Console.WriteLine("thread pool worker thread id: {0}", threadId);
Console.WriteLine(result);
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.ReadKey();
}
private delegate string RunOnThreadPool(out int threadId);
private static void Callback(IAsyncResult ar)
{
Console.WriteLine("starting a callback...");
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.WriteLine("starting passed to a callback:{0}", ar.AsyncState);
Console.WriteLine("is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
Console.WriteLine("thread pool worker thread id: {0}", Thread.CurrentThread.ManagedThreadId);
}
private static string Test(out int threadId)
{
Console.WriteLine("starting...");
Console.WriteLine("is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
Thread.Sleep(TimeSpan.FromSeconds(2));
threadId = Thread.CurrentThread.ManagedThreadId;
return string.Format("thread pool worker thread id was:{0}", threadId);
}
}
}
相关文章推荐
- C#属性简写用法 {get;set}
- C#Winform中运用DevExpress提供的ChartControl控件绘制折线统计图
- 对象的排序
- 【C#】基础知识—流程控制语句
- 【C#】基础知识—运算符
- 【C#】基础知识—数据类型、数据类型转换
- C# 获取mp3文件的歌曲时间长度
- c#中输入文本文字,将输入的文字生成图片
- C# const和statci readonly区别
- c# 通过反射调用类的构造函数
- C#数据类型
- C#关键字
- 转<<C#集合Dictionary中按值的降序排列
- c#语言学习
- C# 索引器
- C# 退出应用程序办法
- C#中数组、ArrayList和List三者的区别
- C# excel文件读取
- c#实现ping
- C#中的Dictionary简介