简体   繁体   English

如何配置计时器和thread.sleep,以便thread.sleep将等到所有代码执行完毕

[英]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. 我已经编写了一个小型Windows服务,该服务将读取Windows应用程序创建的xml文件并保存到特定位置。 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. xml文件包含多个开始时间,结束时间和执行时间,根据这些时间,我的Windows服务将通过查询sql server数据库来创建Excel工作表。 My problem is in the middle of my code execution thread.sleep is called and my code is not completely executed. 我的问题在代码执行线程中间.sleep被调用,我的代码未完全执行。

my program.cs code : 我的program.cs代码:

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: 我的service1.cs文件具有以下代码:

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. 如何配置我的计时器和thread.sleep,以便我可以避免由于thread.sleep而跳过代码执行。 i would like to run my service for every 15 minutes. 我想每15分钟运行一次我的服务。 can't use task scheduler as per requirement. 不能根据要求使用任务计划程序。

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

replace the line Thread.Sleep(60000); 替换行Thread.Sleep(60000); with await Task.Delay(60000); 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); 就我个人而言,我从不使用Thread.Sleep(),因为您无法快速摆脱它们(例如尝试关闭时); I would recommend using AutoResetEvent's for both the "Sleep" functionality and to use them to see when the other code is done. 我建议同时为“睡眠”功能使用AutoResetEvent,并使用它们来查看其他代码何时完成。 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. PS:我不确定谁会继续评价问题。 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... 特别是因为实际上没有任何关于为什么有人拒绝的解释。人们应该尝试的不仅是成为一个混蛋,然后逃跑,还应该提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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