开发者

Schedule a windows service to run every two hours

开发者 https://www.devze.com 2023-02-23 00:10 出处:网络
I have created the Window Service VS2010, Now i want to Schedul开发者_运维问答e that service to run after every 2Hour. For this what is the code..You could use the RegisterWaitForSingleObject method i

I have created the Window Service VS2010, Now i want to Schedul开发者_运维问答e that service to run after every 2Hour. For this what is the code..


You could use the RegisterWaitForSingleObject method in the ThreadPool class.

Your service code should look something like this (stolen heavily from here and here) :

private ManualResetEvent resetEvent = new ManualResetEvent(false);
private RegisteredWaitHandle handle;

public void OnStart()
{
    resetEvent.Reset();
    handle = ThreadPool.RegisterWaitForSingleObject(resetEvent, callBack, null, 7200000, false);
}

public void OnStop()
{
    reset.Set();
}

private void callBack(object state, bool timeout)
{
    if (timeout)
    {
        //Do Stuff Here
    }
    else
    {
        handle.Unregister(null);
    }   
}


In the Debug or Release folder, you will have an .exe version of the file.

Use Task scheduler to run it whenever you want to.

On Windows XP and Server 2003 you can access this from the Start Menu and clicking on Settings and then Control Panel to Scheduled Tasks

The above will constantly utilize your resources. For intermittent usage PS Service can do the job: http://technet.microsoft.com/en-us/sysinternals/bb897542.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消