简体   繁体   English

关于Thread.Sleep()2小时在C#中的问题

[英]Issue regarding Thread.Sleep() for 2hours in c#

I am working on .net windows application. 我正在.net Windows应用程序上工作。

I am using System.Threading.Thread. 我正在使用System.Threading.Thread.

In a single form using five(5) threads. 使用五(5)个线程的单一形式。 I have a code which when run, it executes series of lines in sequence. 我有一个代码,它在运行时会按顺序执行一系列行。 I would like to add a pause in between. 我想在两者之间添加一个暂停。

For that i am using 为此,我正在使用

Thread.Sleep(10800000)

of 3 hours 3小时

But I checked in debug mode, after executing line of 但是我在执行以下命令后检查了调试模式

Thread.Sleep(10800000)

My debug not goes to next line or next line never executes even after waiting for 4 hours. 我的调试未进入下一行,或者即使等待了4个小时,下一行也从未执行。

I am using this Thread.Delay in other thread not in main thread. 我在其他线程不在主线程中使用此Thread.Delay。 This delay requires because, i send a command to configure setting to a hardware, that setting requires minimum 3 hours to complete. 此延迟需要,因为我向硬件发送配置配置命令,该设置至少需要3个小时才能完成。

That's why i am using this 这就是为什么我用这个

Thread.Delay(10800000)

Means my onward code is proceed only after waiting for 3 hours. 意味着我的后续代码仅在等待3个小时后才能继续执行。

Can any one help me? 谁能帮我?

Thread.Sleep is not designed for long sleeps. Thread.Sleep不适用于长时间睡眠。 You should consider using somthing like System.Threading.Timer . 您应该考虑使用诸如System.Threading.Timer之类的东西

Provides a mechanism for executing a method on a thread pool thread at specified intervals. 提供一种机制,用于以指定的时间间隔在线程池线程上执行方法。

You can give it a first run time of midnight, and have it go off every 24 hours. 您可以给它一个午夜的首次运行时间,并使其每24小时关闭一次。 the Timer(TimerCallback, Object, TimeSpan, TimeSpan) constructor is exactly what you are looking for. Timer(TimerCallback,Object,TimeSpan,TimeSpan)构造函数正是您要寻找的。

One would argue that even using a timer is not the best tool for the job. 有人会说,即使使用计时器也不是完成这项工作的最佳工具。 You may want to consider using Windows Task Scheduler (TS) instead. 您可能要考虑使用Windows Task Scheduler (TS)。 With TS, you can setup a schedule to say run your app and carry out the workflow, or if your program must run all the time, trigger another process that communicates with your app somehow. 使用TS,您可以设置时间表以说运行您的应用并执行工作流程,或者如果您的程序必须一直运行,则触发另一个以某种方式与您的应用进行通信的进程。

If the process is not doing anything until the next interval then it's best to just simply kill the app . 如果该过程在下一个间隔之前什么也不做,那么最好只是终止该应用程序 That way you won't be wasting threads or processes twiddling their thumbs over exorbitant delays waiting for the next interval to do something. 这样,您就不会浪费线程或进程在等待下一个间隔执行某项操作之前,将它们的拇指摆在过度的延迟上。

You can use the Microsoft Task Scheduler v2 COM Library from c# to setup your schedules or do so manually from the TS UI itself. 您可以使用c#中的Microsoft Task Scheduler v2 COM库来设置计划,也可以从TS UI本身手动进行。

I suggest using a timer instead of Thread.Sleep or Delay. 我建议使用计时器而不是Thread.Sleep或Delay。 In place of Thread.Sleep start a timer with timeout of 3 or 4 hours depending on your needs which then would send needed command. 代替Thread.Sleep,根据您的需要启动一个3或4个小时超时的计时器,然后该计时器将发送所需的命令。

System.Threading.Timer might do the job System.Threading.Timer可能会做

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

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