打点计时器实验:实验分析C#中三种计时器使用异同点来源: 发布时间:星期四, 2009年2月12日 浏览:83次 评论:0
中提供了 3种类型 ![]() 1、基于 ![]() ![]() 2、基于 ![]() ![]() 3、线程计时器( ![]() 下面我就通过 ![]() ![]() ![]() ![]() ![]() 实验例子截图: ![]() ![]() ![]() ![]() 首先注意 ![]() ![]() 此计时器从Visual Basic 1.0 版起就存在于该产品中 ![]() 这个计时器是使用最简单 ![]() ![]() ![]() ![]() ![]() ![]() 实验出来 ![]() ![]() 1、当启动此计时器后 ![]() ![]() private void formsTimer_Tick(object sender ![]() { i ![]() lblSubThread.Text ![]() ![]() ![]() ![]() } ![]() 2、当单击主线程暂停5秒后 ![]() ![]() ![]() ![]() ![]() ![]() 3、在子进程 ![]() 4、定义 ![]() [ThreadStatic] private ![]() ![]() 在子线程事件中每次加 ![]() ![]() ![]() 2、基于 ![]() ![]() ![]() ![]() ![]() ![]() ![]() 在VS2005 ![]() ![]() ![]() 使用方式有两种 ![]() 1、通过SynchronizingObject属性依附于窗体 ![]() ![]() ![]() timersTimer.Enabled = false; timersTimer.Interval = 100; timersTimer.Elapsed ![]() ![]() ![]() timersTimer.SynchronizingObject = this; 通过这种方式来使用 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 2、不使用SynchronizingObject属性 这种方式就是多线程 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() delegate void SetTextCallback( ![]() ![]() ![]() void timersTimer_Elapsed(object sender ![]() ![]() { //使用代理 ![]() ![]() ![]() ![]() SetTextCallback d = ![]() this.Invoke(d ![]() ![]() ![]() i ![]() } private void SetText( ![]() { lblSubThread.Text ![]() } 这样我们再次实验就会得到如下 ![]() 1、当启动此计时器后 ![]() ![]() ![]() 2、当单击主线程暂停5秒后 ![]() ![]() ![]() ![]() ![]() 3、在子进程 ![]() 4、在子线程事件中每次给线程静态变量加 ![]() ![]() ![]() ![]() 3、线程计时器( ![]() 线程计时器也不依赖窗体 ![]() ![]() ![]() ![]() ![]() ![]() 对消息不在线程上发送 ![]() ![]() ![]() ![]() 使用思路方法如下: ![]() public void ThreadMethod(Object state) { //使用代理 ![]() ![]() ![]() ![]() SetTextCallback d = ![]() this.Invoke(d ![]() ![]() ![]() i ![]() } private void Form1_Load(object sender ![]() { threadTimer = ![]() ![]() ![]() ![]() ![]() ![]() ![]() } 暂停代码: threadTimer.Change(-1 ![]() 实验 ![]() ![]() ![]() ![]() ![]() ![]() ![]() 当然具体 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 下面列出老外整理总结 ![]() ![]() ![]() Feature description ![]() ![]() ![]() Support for adding and removing listeners after the timer is instantiated. Yes No Yes Supports call backs on the user- ![]() Calls back from threads obtained from the thread pool Yes Yes No Supports drag-and-drop in the Windows Forms Designer Yes No Yes Suitable for running in a server multi-threaded environment Yes Yes No Includes support for passing arbitrary state from the timer initialization to the callback. No Yes No Implements IDisposable Yes Yes Yes Supports one-off callbacks as well as periodic repeating callbacks Yes Yes Yes Accessible across application do ![]() Supports IComponent – hostable in an IContainer Yes No Yes 0
相关文章读者评论发表评论 |
|