我要作一个winform程序,实现一个数据更新的功能。要怎么样设定这个程序的自动执行时间呢?

意思就是,我将这个程序跑起来,他会隔几个小时自动运行一次,或者说在指定的时间去运行一次。怎么做呢?哪位大虾指点下。

Winform的话,用Timer就好了,拖一个Timer控件到窗体上(放到任意位置,无影响),然后在Timer的Tick事件中写你需要做哪些判断以及后续动作;Interval属性代表Tick事件的触发频率,单位是毫秒,你需要几小时自动运行一次就自己按需设置;最后,别忘了把Enable设为true,为false的时候Timer的Tick事件不触发。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-08-16
使用timer,标准的做法做成windows服务

大致的代码类似如下
private System.Timers.Timer timer = new System.Timers.Timer();
public Service()
{
InitializeComponent();
this.timer.Elapsed += new ElapsedEventHandler(this.timer_Elapsed);
}

protected override void OnStart(string[] args)
{
timer.Enabled = true;
timer.Interval = 10000;
timer.Start();
}

protected override void OnStop()
{
timer.Stop();
}

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer.Stop();
timer.Interval = 60000; //1分钟
timer.Start();
Start();
}

private void Start()
{
//打算实现的逻辑
}本回答被提问者采纳
第2个回答  2011-08-16
一定要Winform么?做成Windows Service可以么?
如果Winform,可以加Timer来处理,时间间隔设置好;
或者在Windows里设置【任务计划】来定时调用你的程序。追问

没用WindowsService做过,就请问下在Winform下Timer要怎么设置,怎么应用呢?

追答

你使用Timer控件最简单了,如图,然后设置它的Interval属性,这个是以微秒为单位的,然后你双击这个控件添加事件,
private void tmrTimer_Tick(object sender, EventArgs e)
{
try
{
ToDo......
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

第3个回答  2011-08-16
01./// <summary>
02. /// 定时器
03. /// </summary>
04. public void TimeDo()
05. {
06. System.Timers.Timer aTimer = new System.Timers.Timer();
07. aTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimeEvent);
08. aTimer.Interval = 1000;
09. aTimer.Enabled = true;
10. }
11. /// <summary>
12. /// 定时器触发事件
13. /// </summary>
14. /// <param name="source"></param>
15. /// <param name="e"></param>
16. private static void TimeEvent(object source, System.Timers.ElapsedEventArgs e)
17. {
18. int intHour = e.SignalTime.Hour;
19. int intMinute = e.SignalTime.Minute;
20. int intSecond = e.SignalTime.Second;
21. int iHour = 10;
22. int iMinute = 30;
23. int iSecond = 00;
24. // 设置  每秒钟的开始执行一次
25. if (intSecond == iSecond)
26. {
27. Console.WriteLine("每秒钟的开始执行一次!");
28. }
29. // 设置 每个小时的30分钟开始执行
30. if (intMinute == iMinute && intSecond == iSecond)
31. {
32. Console.WriteLine("每个小时的30分钟开始执行一次!");
33. }
34. // 设置 每天的10:30:00开始执行程序
35. if (intHour == iHour && intMinute == iMinute && intSecond == iSecond)
36. {
37. Console.WriteLine("在每天10点30分开始执行!");
38. }
39. }

摘自:http://blog.csdn.net/malimalihun/article/details/6657239
相似回答