循环计时器 [英] Loop in a timer
本文介绍了循环计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你们好..
你能帮我吗?
我试图每小时,每分钟或每秒钟执行另一个设置为启动计时器进行计数的进程.
请帮忙..
Hello Guys..
Can you please help me?
I''m trying to execute another process every hour or minute or seconds which I have set to start my timer to count.
Please help..
thanks in advanced!!!
推荐答案
我写了一个小程序,其中我的调度程序每分钟调用一次显示程序.希望对您有帮助.
显示程序:
I have written small program in which my scheduler program calls display program per minute. I hope it will help you.
Display Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
Console.ReadLine();
}
}
}
调度程序
Scheduler Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
namespace ConsoleApplication3
{
class Scheduler
{
System.Timers.Timer processTimer;
public TimeSpan NextRunTime = TimeSpan.Zero;
TimeSpan scheduleTime;
static void Main(string[] args)
{
Scheduler obj = new Scheduler();
obj.SetArchiveProcessScheduleTime();
}
private void SetArchiveProcessScheduleTime()
{
processTimer = new Timer();
processTimer.Start();
scheduleTime=TimeSpan.Parse(DateTime.Now.ToString("hh:mm"));
processTimer.Interval = 100;
processTimer.Enabled = true;
processTimer.Elapsed += new ElapsedEventHandler(processTimer_Elapsed);
while(true)
{
}
}
private void processTimer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
processTimer.Enabled = false;
//Invoke exe
System.Diagnostics.Process.Start(@"C:\Pallavi\Test Code\DisplayTime\ConsoleApplication3\bin\Debug\ConsoleApplication3.exe");
Console.WriteLine("Another process has been invoked: "+DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
#region Set Next Run Time for Archiving Process
TimeSpan timeDiff = TimeSpan.Zero;
NextRunTime = TimeSpan.Zero;
if (NextRunTime == null || NextRunTime.Equals(TimeSpan.Zero))
{
NextRunTime = scheduleTime.Add(new TimeSpan(0, 0, 1, 0));//here you can specify whether you want to run per day/hour/minute/second
timeDiff = NextRunTime.Subtract(scheduleTime);
}
if (timeDiff.TotalMilliseconds <= 0 || timeDiff.TotalMilliseconds >= Int32.MaxValue)
processTimer.Interval = 1000;
else
processTimer.Interval = timeDiff.TotalMilliseconds;
#endregion Set Next Run Time for Archiving Process
processTimer.Enabled = true;
}
catch (Exception ex)
{
}
}
}
}
这篇关于循环计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文