如何在多线程编程中下午4点启动一个线程 [英] how to start a thread at 4pm in Multithread programming

查看:79
本文介绍了如何在多线程编程中下午4点启动一个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用C#windows编程,它连续运行,但如果它是下午4点。然后它应该调用另一个模块并在正在运行的程序之间处理它。





感谢所有

Hi All,
I am using a C# windows programming which running behind continuously but if it is sharp 4 p.m. then it should call another module and process it in-between the running program.


Thanks To All

推荐答案

没有调用模块这样的概念,但是你可以启动一个过程;但是,它与线程无关。但是如果你真的想要(我认为你没有任何精心设计和合理的计划),你可以在预设的时刻开始一个主题。



F-ES Sitecore在对这个问题的评论中提出的建议是错误的。处理每个滴答是浪费的方法,可以解决你的严重问题。这绝对没有必要。您只需使用计时器并使用 System.DateTime.Now 查询当前天文时间。创建对应于下午4点的另一个时间实例。另一天的同一天。如果你从第一次减去第二次( System.DateTime.Now 为此目的定义了运算符' - ',你将得到时间的对象 System.TimeSpan 表示当前时间与您需要计时器调用事件的时间之间的持续时间。使用此持续时间进行计时器设置。



请参阅:

https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.timespan%28v=vs.110%29.aspx [ ^ ],

this是您要使用的计时器类: https:/ /msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx [ ^ ],

这是您在计时器事件之前发送间隔的方式:https://msdn.microsoft.com/en-us/library/system.timers .timer.interval%28v = vs.110%29.aspx [ ^ ],

这是你想要处理的计时器事件:https://msdn.microsoft.com/en-us/library/ system.timers.timer.elapsed(v = vs.110).aspx [ ^ ]。



另外一个细节:当你可以看到 Interval 以毫秒为单位设置。这是你从 TimeSpan 实例计算毫秒数的方法:

https://msdn.microsoft.com/en-us/library/system.timespan.totalmilliseconds(v = vs.110)的.aspx [ ^ ]

https://msdn.microsoft.com/en-us/library/system.timespan.milliseconds(v = vs.110).aspx [ ^ ]。



但是,如果你让一些应用程序一直运行,这只会很好。如果您不想要或不需要它,还有另一种方法:使用一些可用的服务应用程序。此类服务是Windows任务计划程序。有关详细信息,请参阅我之前的回答:问题窗口服务 [< a href =http://www.codeproject.com/Answers/314439/Problem-a-Window-Services#answer1target =_ blanktitle =New Window> ^ ]。







关于F-ES Sitecore在时移上的讨论的一些摘要。不应该考虑在设置定时器和定时器事件之间的时间段内随机无动机的时钟变化;这将是荒谬的。换句话说,垃圾进入 - 垃圾输出。将所有用户操作考虑在内的要求几乎与有人破坏计算机时进行正确计算的要求一样荒谬。请参阅下面的评论。



要解决的唯一合法问题是考虑夏季节省时间。可以在提前卷绕定时器之前考虑它。这足以计算启动计时器和计时器事件时的时刻,并与转换结果进行比较:

https://msdn.microsoft.com/en-us/library/system.datetime.tolocaltime.aspx

https://msdn.microsoft.com/en -us / library / system.timezone.getutcoffset.aspx



-SA
There is no such concept as "calling a module", but you can start a process; however, it has nothing to do with threading. But if you really want (I don't think you have any elaborate and reasonable plan though), you can start a thread at some preset moment of time.

The advice by F-ES Sitecore in his comment to the question is wrong. Handling each tick is way to wasteful and can get you serious problems. It's absolutely not necessary. You simply use the timer and inquire current astronomical time using System.DateTime.Now. Create another time instance corresponding to 4 p.m. of the same of another day. If you subtract second time from the first one (System.DateTime.Now has the operator '−' defined for this purpose), you will get the object of the time System.TimeSpan representing the time duration between current time and the time you need your timer to invoke the event. Use this duration for the timer setting.

Please see:
https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.timespan%28v=vs.110%29.aspx[^],
this is the timer class you want to use: https://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx[^],
this is how you send the interval before the timer event: https://msdn.microsoft.com/en-us/library/system.timers.timer.interval%28v=vs.110%29.aspx[^],
this is the timer event you want to handle: https://msdn.microsoft.com/en-us/library/system.timers.timer.elapsed(v=vs.110).aspx[^].

One more detail: as you can see Interval is set in milliseconds. This is how you calculate milliseconds from your TimeSpan instance:
https://msdn.microsoft.com/en-us/library/system.timespan.totalmilliseconds(v=vs.110).aspx[^]
or https://msdn.microsoft.com/en-us/library/system.timespan.milliseconds(v=vs.110).aspx[^].

But this is only good if you keep some application running all that time. If you don't want or need it, there is another way: using some available service application. Such service is Windows Task Scheduler. For further detail, please see my past answer: Problem a Window Services[^].



Some summary of the discussion with F-ES Sitecore on time shift. Random non-motivated change of the clock during the time period between setting the timer and timer event just should not be taken into account; it would be just absurd. In other words, garbage in — garbage out. The requirement to take all user actions into account is nearly as absurd as the requirement to do correct computing if someone breaks the computer. Please see my comments below.

The only legitimate problem to address would be taking into account summer saving time. It can be taken into account before winding the timer, in advance. It's enough to calculate the moments of time at the time of starting the timer and timer event and compare with the result of conversions:
https://msdn.microsoft.com/en-us/library/system.datetime.tolocaltime.aspx
https://msdn.microsoft.com/en-us/library/system.timezone.getutcoffset.aspx

—SA


这篇关于如何在多线程编程中下午4点启动一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆