Piy*_*ana 2 c# windows-services
我创建在Visual Studio 2010中使用C#中的窗口服务,我有几个方法,比如Add,Multiply和其他人,我敢投入的onStart方法.现在,我希望这些方法每五分钟运行一次.那么后台工作者流程如何帮助我呢?
protected override void OnStart(string[] args)
{
add(); // yes, it doesn't have parameters
}
Run Code Online (Sandbox Code Playgroud)
在类中包装这些函数并在该类中创建System.Timers.Timer()并在该计时器中调用所有这些函数.调用OnStart服务的示例类NewClass的Start()函数.
class NewClass
{
this._watcherTimer = new System.Timers.Timer();
this._watcherTimer.Interval = 60000;
this._watcherTimer.Enabled=False;
this._watcherTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.Timer_Tick);
public void Start()
{
this._watcherTimer.Enabled=true;
}
private void Timer_Tick(object sender, EventArgs e)
{
Add();
Multiply();
}
}
Run Code Online (Sandbox Code Playgroud)
计时器是正确的选择。我有一个稍微增强的版本,可以处理OnStop方法中计时器的关闭。
在您的program.cs中,我将执行以下操作以简化调试过程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
namespace SampleWinSvc
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
#if (!DEBUG)
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new Service1() };
ServiceBase.Run(ServicesToRun);
#else
//Debug code: this allows the process to run
// as a non-service. It will kick off the
// service start point, and then run the
// sleep loop below.
Service1 service = new Service1();
service.Start();
// Break execution and set done to true to run Stop()
bool done = false;
while (!done)
Thread.Sleep(10000);
service.Stop();
#endif
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在您的Service1.cs代码中:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Timers;
using System.Text;
namespace SampleWinSvc
{
public partial class Service1 : ServiceBase
{
/// <summary>
/// This timer willl run the process at the interval specified (currently 10 seconds) once enabled
/// </summary>
Timer timer = new Timer(10000);
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Start();
}
public void Start()
{
// point the timer elapsed to the handler
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
// turn on the timer
timer.Enabled = true;
}
/// <summary>
/// This is called when the service is being stopped.
/// You need to wrap up pretty quickly or ask for an extension.
/// </summary>
protected override void OnStop()
{
timer.Enabled = false;
}
/// <summary>
/// Runs each time the timer has elapsed.
/// Remember that if the OnStop turns off the timer,
/// that does not guarantee that your process has completed.
/// If the process is long and iterative,
/// you may want to add in a check inside it
/// to see if timer.Enabled has been set to false, or
/// provide some other way to check so that
/// the process will stop what it is doing.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
MyFunction();
}
private int secondsElapsed = 0;
void MyFunction()
{
secondsElapsed += 10;
}
}
}
Run Code Online (Sandbox Code Playgroud)
通过在编译选项中设置#DEBUG var,您可以自己将代码作为程序运行,然后在准备测试关闭逻辑时,只需将所有内容破坏并设置为true。多年来,我一直使用这种方法取得了很大的成功。
如代码中所述,如果您在计时器事件中做任何长时间的事情,那么您可能希望通过OnStop对其进行监视,以确保它在中间关闭时有足够的时间。
| 归档时间: |
|
| 查看次数: |
14301 次 |
| 最近记录: |