除了C#中的计时器之外,哪种方法是在Windows服务中定期调用函数的最佳方法 [英] Which is the best way of calling a function periodically in windows service other than timer in C#

查看:57
本文介绍了除了C#中的计时器之外,哪种方法是在Windows服务中定期调用函数的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要在Windows服务应用程序中定期调用函数(带开始时间和间隔)。除了计时器和Windows任务调度器有没有更好的方法?请建议。谢谢。



我尝试过:



是可能使用异步任务?

解决方案

不知道它是否符合您的要求。但它对我有用。

  public   void  InitJob()
{
动作作业=()= > {Console.WriteLine(


< blockquote> 正在运行{DateTime.Now}); };
Func< DateTime> computeNextStart =()= >
{
var now = DateTime.Now ;
返回 now.Date
+ TimeSpan.FromHours(now.Hour)
+ TimeSpan.FromMinutes(now.Minute + 1 );
};
ThreadPool.QueueUserWorkItem(
(state)= > ExecuteJobTimedAsync(job,computeNextStart));
}

private static async void ExecuteJobTimedAsync(
动作作业,
Func< DateTime> computeNextStart)
{
DateTime waitUntil = computeNextStart();
TimeSpan wait = waitUntil - DateTime.Now;
if (等待> TimeSpan.Zero) await Task.Delay(wait).ConfigureAwait( false );
job();
ThreadPool.QueueUserWorkItem(
(state)= > ExecuteJobTimedAsync(job,computeNextStart));
}



该示例每隔一分钟执行一次作业(参见computeNextStart);虽然工作时间不会超过一分钟。


最好的方法是利用线程来达到这样的要求。

- 创建一个线程

- 确保一次只运行一个线程

- 执行所需的操作

- 一旦操作完成睡眠间隔中提到的时间线程

- 重复该过程

- 同时检查当前是否正在进行任何操作,同时停止服务并相应地等到它完成



尝试这种方法。如果你需要更多细节来实现这种方法,请告诉我。



希望,它有帮助:)


Hi,
I need to call a function periodically (with start time and an interval) in windows service application. Other than timer and windows task scheduler is there any better way? Pls suggest.Thanks.

What I have tried:

Is that possible using async task?

解决方案

Don't know if it fits your requirements. But it works to me.

public void InitJob() 
{
    Action job = () => { Console.WriteLine(


"Running {DateTime.Now}"); }; Func<DateTime> computeNextStart = () => { var now = DateTime.Now; return now.Date + TimeSpan.FromHours(now.Hour) + TimeSpan.FromMinutes(now.Minute + 1); }; ThreadPool.QueueUserWorkItem( (state) => ExecuteJobTimedAsync(job, computeNextStart)); } private static async void ExecuteJobTimedAsync( Action job, Func<DateTime> computeNextStart) { DateTime waitUntil = computeNextStart(); TimeSpan wait = waitUntil - DateTime.Now; if (wait > TimeSpan.Zero) await Task.Delay(wait).ConfigureAwait(false); job(); ThreadPool.QueueUserWorkItem( (state) => ExecuteJobTimedAsync(job, computeNextStart)); }


The example executes the job every full minute (see computeNextStart); while the job does not take longer than a minute.


The best way is to make use of threading to achieve such requirement.
-- create a thread
-- make sure that there is only one thread running at once
-- do the required operation
-- once the operation is finished sleep thread for the time mentioned in interval
-- repeat the process
-- also check whether there is any operation currently going on while stopping the service and accordingly wait till it finishes

Try this approach. In case you need more details implementing this approach, please let me know.

Hope, it helps :)


这篇关于除了C#中的计时器之外,哪种方法是在Windows服务中定期调用函数的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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