简体   繁体   中英

Run method after defined time on windows service

My Question is more about getting to the same destination, but there must be another way. Right now im creating a DateTime and compare that to another DateTime and check, if the time difference I set up might be right. So far so good but I just can't accept that I create a new propertie everytime the loop will get into that code.

Is there any possible way to get to the same destination, but in some kind of more effective way?

I got you guys some example code here:

        private void RunService()
        {
            // Runs as long as the service didn't got a stop call.
            while (!SetStop)
            {
                //Get MinutesToWait
                this.MinutesToWait = 5;

                DateTime CheckRunTime = this.LastRun;
                CheckRunTime.AddMinutes(this.MinutesToWait);

                if (DateTime.Now >= CheckRunTime)
                {
                    // Imagine some good and smart and totally runnable code?
                }
            }
        }

If I understood correctly, what you want to do is execute a piece of code some time after the service starts. If that is the case, then your best bet would be to use a timer .

First off, you have to convert the amount of time you want to wait to milliseconds. For example, 5 minutes equals 300000ms. Then, you have to move the code you want to execute to a separate method. I will name this method RunCode() for the example. Finally, you create your timer like so:

private void RunService()
{
    var timer = new Timer(300000);
    timer.Elapsed += (s, e) => this.RunCode();
    timer.Start();
    Thread.Sleep(Timeout.Infinite);
}

What we are doing here is the following.

  1. Instantiating the timer with an interval of 300000ms
  2. Subscribing to the timer's Elapsed event, which fires when the specified time has passed
  3. Starting the timer
  4. Sleeping our main thread forever. This line is optional. Whether you need it or not depends on the structure of your program. If nothing else happens in your code after you start the timer, the main thread and by extension the whole program will exit. However by sleeping it with an infinite timeout, we can prevent that.

If you're sure that this, delayed execution of code, is what you want, then the solution I have provided should work quite well. However I'm worried this may be an XY problem , meaning this is the solution you have come up with for a different issue which could be solved better. So I have to ask, why exactly do you need this in your service?

Use System.Timers

static void Main(string[] args) {

        Timer T = new Timer();
        T.Elapsed += Run;
        T.Interval = 100;
        T.Start();

}
static void Run(object source, ElapsedEventArgs e) {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM