首页 新闻 会员 周边

winform定时运行怎么做,比如每天12点的时候执行一段代码

0
[已关闭问题] 关闭于 2012-02-16 13:11

winform定时运行怎么做,比如每天12点的时候执行一段代码

拖鞋王子的主页 拖鞋王子 | 初学一级 | 园豆:37
提问于:2011-11-29 15:13
< >
分享
所有回答(5)
0

使用Timer:http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

Greatest | 园豆:678 (小虾三级) | 2011-11-29 15:16

有代码否

支持(0) 反对(0) 拖鞋王子 | 园豆:37 (初学一级) | 2011-11-29 15:17

@拖鞋王子: 

public class Class1 {
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
static int alarmCounter = 1;
static bool exitFlag = false;

// This is the method to run when the timer is raised.
private static void TimerEventProcessor(Object myObject,
EventArgs myEventArgs) {
myTimer.Stop();

// Displays a message box asking whether to continue running the timer.
if(MessageBox.Show("Continue running?", "Count is: " + alarmCounter,
MessageBoxButtons.YesNo) == DialogResult.Yes) {
// Restarts the timer and increments the counter.
alarmCounter +=1;
myTimer.Enabled = true;
}
else {
// Stops the timer.
exitFlag = true;
}
}

public static int Main() {
/* Adds the event and the event handler for the method that will
process the timer event to the timer.
*/
myTimer.Tick += new EventHandler(TimerEventProcessor);

// Sets the timer interval to 5 seconds.
myTimer.Interval = 5000;
myTimer.Start();

// Runs the timer, and raises the event.
while(exitFlag == false) {
// Processes all the events in the queue.
Application.DoEvents();
}
return 0;
}
}
支持(0) 反对(0) Greatest | 园豆:678 (小虾三级) | 2011-11-29 15:20

@Greatest:  不是每隔多少时间执行,而且指定时间执行

支持(0) 反对(0) 拖鞋王子 | 园豆:37 (初学一级) | 2011-11-29 15:39

@拖鞋王子:

private static void TimerEventProcessor(Object myObject,
EventArgs myEventArgs) {
if (now == 6:00)
{
//.....
}
}



支持(0) 反对(0) Greatest | 园豆:678 (小虾三级) | 2011-11-29 15:50

@Greatest: 那只能每隔一秒判断一次了

支持(0) 反对(0) 拖鞋王子 | 园豆:37 (初学一级) | 2011-11-29 15:57
0

推荐你一个专业的调度框架quartz http://www.cnblogs.com/2018/archive/2011/10/12/2171657.html

2012 | 园豆:21230 (高人七级) | 2011-11-30 12:54
0

给你个思路把,winfrom:用Timer控件,每隔一定的时间去获取当前系统时间,然后判断离12点还剩多少时间,比如5分,就开始每秒去判断,到了12点就开始执行你写的方法(),建议,做一个按钮来停止Timer控件的执行,总不能让Timer一直跑下去啊。

B/S:可以用JavaScript,运行网页就开始判断时间,然后判断是否==12:00。

总结:最好根据客户的使用习惯来写执行代码。

Verms | 园豆:286 (菜鸟二级) | 2011-11-30 16:43
0

嗯 只能用timer每秒执行了

wdwwtzy | 园豆:114 (初学一级) | 2011-11-30 17:39
0

新建windows 任务,每天指定时间启动winform程序 窗体加载的时候执行,执行完退出

MengXQ | 园豆:181 (初学一级) | 2011-12-17 00:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册