简体   繁体   中英

how to configure timer and thread.sleep so that thread.sleep will wait until all the code is executed

I have written a small windows service, which will read xml file created by a windows application and saved to a particular location. the xml file contains multiple start time, end time and execution time based on which my windows service will create a excel sheet by querying the sql server database. My problem is in the middle of my code execution thread.sleep is called and my code is not completely executed.

my program.cs code :

namespace RepService
{
    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
            Service1 myServ = new Service1();
            myServ.Start();
            //Set the Thread to sleep

            Thread.Sleep(60000);
            //Call the Stop method-this will stop the Timer.
            myServ.Stop();
#endif
        }
    }
}

my service1.cs file has following code:

public Service1()
        {
            _aTimer = new System.Timers.Timer(30000);
            _aTimer.Enabled = true;
            _aTimer.Elapsed += new System.Timers.ElapsedEventHandler(_aTimer_Elapsed);
            InitializeComponent();
}
void _aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {//huge code to be executed
}

how to configure my timer and thread.sleep so that i can avoid skipping my code execution because of thread.sleep. i would like to run my service for every 15 minutes. can't use task scheduler as per requirement.

在服务方法的回调中执行thread.sleep

replace the line Thread.Sleep(60000); with await Task.Delay(60000); ?

Personally, I never use Thread.Sleep() because you can't get out of them quickly (like when trying to shut down); I would recommend using AutoResetEvent's for both the "Sleep" functionality and to use them to see when the other code is done. You can do something like this:

public System.Threading.AutoResetEvent doneFlag = new System.Threading.AutoResetEvent(false); // used to signal when other work is done

....

Service1 myServ = new Service1();
myServ.Start();
if(doneFlag.WaitOne(SOME_TIMEOUT))
{
    // doneFlag was set, so other code finished executing
}
else
{
    // doneFlag was not set, SOME_TIMEOUT time was exceeded. Do whatever you want to handle that here
}
....
void _aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    //huge code to be executed
    doneFlag.Set(); // this will trigger the previous code pretty much immediately to continue
}

Hopefully this is what you're looking for. Let me know if you have any questions!

PS: I'm not sure who keeps going around rating down questions. Especially since there's really no explanation for why someone voted it down.. People should try being more helpful than just being a jerk and then running off.. Rated it up to counter-act the jerks...

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