<UserControlx:Class="Silverlight20.Thread.ThreadPool"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanelHorizontalAlignment="Left"Margin="5">
<TextBlockx:Name="txtMsgQueueUserWorkItem"Text="clickhere"MouseLeftButtonDown="txtMsgQueueUserWorkItem_MouseLeftButtonDown"Margin="30"/>
<TextBlockx:Name="txtRegisterWaitForSingleObject"Text="clickhere"MouseLeftButtonDown="txtRegisterWaitForSingleObject_MouseLeftButtonDown"Margin="30"/>
</StackPanel>
</UserControl>
ThreadPool.xaml.cs
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Net;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Animation;
usingSystem.Windows.Shapes;
namespaceSilverlight20.Thread
{
publicpartialclassThreadPool:UserControl
{
publicThreadPool()
{
InitializeComponent();
}
privatevoidtxtMsgQueueUserWorkItem_MouseLeftButtonDown(objectsender,MouseButtonEventArgse)
{
//ThreadPool-线程池的管理类
//QueueUserWorkItem(WaitCallbackcallBack,Objectstate)-将指定方法加入线程池队列
//WaitCallbackcallBack-需要在新开线程里执行的方法
//Objectstate-传递给指定方法的参数
System.Threading.ThreadPool.QueueUserWorkItem(DoWork,DateTime.Now);
}
privatevoidDoWork(objectstate)
{
//作为线程管理策略的一部分,线程池在创建线程前会有一定的延迟
//也就是说线程入队列的时间和线程启动的时间之间有一定的间隔
DateTimedtJoin=(DateTime)state;
DateTimedtStart=DateTime.Now;
System.Threading.Thread.Sleep(3000);
DateTimedtEnd=DateTime.Now;
//Dispatcher.BeginInvoke()-在与Dispatcher相关联的线程上执行指定的操作。自动线程同步
this.Dispatcher.BeginInvoke(()=>
{
txtMsgQueueUserWorkItem.Text+=string.Format("\r\n入队列时间{0}启动时间{1}完成时间{2}",
dtJoin.ToString(),dtStart.ToString(),dtEnd.ToString());
});
}
privatevoidtxtRegisterWaitForSingleObject_MouseLeftButtonDown(objectsender,MouseButtonEventArgse)
{
System.Threading.AutoResetEventdone=newSystem.Threading.AutoResetEvent(false);
//为了传递RegisteredWaitHandle对象,要将其做一个封装
RegisteredWaitHandlePacketpacket=newRegisteredWaitHandlePacket();
//RegisterWaitForSingleObject-注册一个WaitHandle。在超时或发信号的情况下对指定的回调方法做调用
//第一个参数:需要注册的WaitHandle
//第二个参数:需要回调的方法(此处省略掉了委托类型)
//第三个参数:传递给回调方法的参数
//第四个参数:超时时间(到超时时间则调用指定的方法)
//第五个参数:是否为一次调用(是到超时时间一次性调用指定的方法,还是每次超时时间后都调用指定的方法)
packet.Handle=System.Threading.ThreadPool.RegisterWaitForSingleObject
(
done,
WaitOrTimer,
packet,
100,
false
);
System.Threading.Thread.Sleep(555);
done.Set();//发出信号,调用RegisterWaitForSingleObject所指定的方法
}
publicvoidWaitOrTimer(objectstate,booltimedOut)
{
RegisteredWaitHandlePacketpacket=stateasRegisteredWaitHandlePacket;
//booltimedOut-是否是因为超时而执行到这里
if(!timedOut)
{
//如果不是因为超时而执行到这里(即因为AutoResetEvent发出了信号而执行到这里),则注销指定的RegisteredWaitHandle
packet.Handle.Unregister(null);
}
this.Dispatcher.BeginInvoke(()=>
{
txtRegisterWaitForSingleObject.Text+=
String.Format("\r\n是否收到信号:{0}",(!timedOut).ToString());
});
}
}
/**////<summary>
///封装了RegisteredWaitHandle的类
///</summary>
publicclassRegisteredWaitHandlePacket
{
publicSystem.Threading.RegisteredWaitHandleHandle{get;set;}
}
}
优质内容筛选与推荐>>
1、android异常总结三 :R文件丢失2、什么是mvvm3、win 2003 安装 vs2005 sp1 问题17184、 世界杯与国产开源(一) 5、C#中抽象类和接口的区别与使用
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。