




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第30课 多线程编程技术32010-08-13 20:47:47|分类: Winform学习笔记 |标签:timer线程textvoidinvoke |字号大中小订阅 使用线程操作Windows窗体Timer控件命名空间:System.Windows.Forms属性Enabled 设置计时器是否有效Interva 计时时间间隔,以毫秒为单位事件Tick达到时间间隔时触发事件方法Start 开始计时器Stop 关闭计时器使用子线程访问UI-问题与解决方法解决方法1:Control.CheckForIllegalCrossThreadCalls=false; /检查是否可以跨线程调用解决方法2:Invoke/BeginInvokeControl类的方法原型public Object Invoke(Delegate method)public ObjectBeginInvoke(Delegate method)相同点-目标相同调用委托实例方法,使其在该控件所在的线程上执行调用Invoke前判断是否需要跨线程调用UI举例: if (txtShowTime.InvokeRequired) /txtShowTime.Invoke(new DelegateUI(showTime); txtShowTime.BeginInvoke(new DelegateUI(showTime); Thread.Sleep(1000);/BeginInvoke else txtShowTime.Text = DateTime.Now.ToString(); 测试Invoke内容是否有问题不同点Invoke:同步执行BeginInvoke:异步执行实例:按钮移动实例:时间倒计时使用线程实例:随机小球正确的选择使用.Net中的三个TimerTimer这个类在.net的类库中有3个1)System.Threading.Timer 是一个使用回调方法的计时器,而且由线程池线程服务,简单且对资源要求不高。 2)System.Windows.Forms.Timer 这是一个必须和Windows窗体一起使用的Timer。 3)System.Timers.Timer 基于服务器计时器功能的Timer,根据服务器系统时间进行运行的Timer。如果需要写Windows Services的话可以使用这个Timer来进行一 些需要在一定间隔时间进行某项操作的环境下使用。 它使您能够指定在应用程序中引发 Elapsed 事件的周期性间隔。然后可以操控此事件以提供定期处理。例如,假设您有一台关键性服务 器,必须每周 7 天、每天 24 小时都保持运行。可以创建一个使用Timer 的服务,以定期检查服务器并确保系统开启并在运行。如果系统不响应,则该服务可以尝试重新启动服务器或通知管理员。基于服务器的Timer 是为在多线程环境中用于辅助线程而设计的。服务器计时器可以在线程间移动来处理引发的 Elapsed 事件,这样就可以比 Windows 计时器更精确地按时引发事件。 实例:控制台2秒钟输出1次Hello World!-System.Timers.Timer 代码:using System;using System.Timers;namespace TimerTest class Program static void Main(string args) / Normally, the timer is declared at the class level, so / that it doesnt go out of scope when the method ends. / In this example, the timer is needed only while Main / is executing. However, KeepAlive must be used at the / end of Main, to prevent the JIT compiler from allowing / aggressive garbage collection to occur before Main / ends. Timer aTimer = new Timer(); / Hook up the Elapsed event for the timer. aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); / Set the Interval to 2 seconds (2000 milliseconds). aTimer.Interval = 2000; aTimer.Enabled = true; Console.WriteLine(Press the Enter key to exit the program.); Console.ReadLine(); / Keep the timer alive until the end of Main. GC.KeepAlive(aTimer); / Specify what you want to happen when the Elapsed event is / raised. private static void OnTimedEvent(object source, ElapsedEventArgs e) Console.WriteLine(Hello World!); -实例:倒计时-System.Threading.Timer 代码:using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading; namespace _002Timer public partial class Form1 : Form public Form1() InitializeComponent(); /Control.CheckForIllegalCrossThreadCalls = false; System.Threading.Timer threadTimer = null; private delegate void InvokeTest(); private void btnBegain_Click(object sender, EventArgs e) this.btnBegain.Enabled = false; TimerCallback timeTrick = new TimerCallback(changeCurrentTimeText); this.lblLeftTime.Text = 10; threadTimer = new System.Threading.Timer(timeTrick, null, 0, 1000); private void invokeMethod() string timeText = this.lblLeftTime.Text; int lefttime = int.Parse(this.lblLeftTime.Text); lefttime-; this.lblLeftTime.Text = lefttime.ToString(); if (lefttime System.Windows.Forms.Timer 代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace TimerTest public partial class Form1 : Form public Form1() InitializeComponent(); private void btnStart_Click(object sender, EventArgs e) lblShowTime.Text = 10; tmrCount.Start(); btnStart.Enabled = false; private void
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 化工生产劳力、材料供应计划策略
- 企事业单位食堂岗位职责流程
- 人教版五年级信息技术课程评价计划
- 班主任班级特色建设计划
- 北师大版八年级上册物理课外活动安排计划
- 美术教研组教材内容更新计划
- 以形象思维理论为翼助力高中物理教学腾飞
- 以废为宝:废旧材料在小学美术教学中的创新融合与实践探索
- 以多元智能理论为钥:开启高中美术“差生”转化新路径
- 以图式理论为翼翱翔大学英语阅读之空
- 静电放电(ESD)及其防护措施培训课件
- 离婚不离家协议书
- 社区干事考试试题及答案
- 2025年建筑工程管理考试试题及答案
- 2025年广西南宁宾阳县昆仑投资集团有限公司招聘笔试参考题库含答案解析
- 2025年军人离婚协议书范本
- DB11∕T045-2025医学实验室质量与技术要求
- 工程造价复审报告书范文
- 《星形胶质细胞》课件
- SAP S4HANA 用户操作手册-成本控制CO操作手册-002-订单成本核算
- 幼儿园2025-2026学年度第一学期园本培训计划
评论
0/150
提交评论