编写轮询Windows服务

编程入门 行业动态 更新时间:2024-10-27 08:35:57
本文介绍了编写轮询Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我通常使用C#编写Windows服务,但是我使用F#进行了尝试.对于像这样的轮询服务,我通常使用我编写的类,该类类似于 BackgroundWorker .它产生一个后台线程,并定期触发一个 OnWork 方法. (完整代码为此处 [github] .)

I usually write Windows services in C# but I'm giving it a go in F#. For a polling serivce, like this one, I ordinarily use a class I've written, which is similar to BackgroundWorker. It spawns a background thread and fires an OnWork method at regular intervals. (Complete code is here [github].)

在F#中是否还有另一种更好或更惯用的方式来做到这一点?编写轮询后台工作程序类或它的内置替代方法可能是一种更好的方法.

Is there another, perhaps better or more idiomatic, way to do this in F#? It could be a better way to write the polling background worker class, or built-in alternatives to it.

根据乔尔的建议,这就是我的想法.

Here's what I came up, based on Joel's suggestion.

module Async = open System.Diagnostics let poll interval work = let sw = Stopwatch() let rec loop() = async { sw.Restart() work() sw.Stop() let elapsed = int sw.ElapsedMilliseconds if elapsed < interval then do! Async.Sleep(interval - elapsed) return! loop() } loop() //Example open System.Threading let cts = new CancellationTokenSource() Async.Start(Async.poll 2000 (fun () -> printfn "%A" DateTime.Now), cts.Token) Thread.Sleep(TimeSpan.FromSeconds(10.0)) cts.Cancel()

使用poll的服务:

type MyService() = inherit System.ServiceProcess.ServiceBase() let mutable cts = new CancellationTokenSource() let interval = 2000 override __.OnStart(_) = let polling = Async.poll interval (fun () -> //do work ) Async.Start(polling, cts.Token) override __.OnStop() = cts.Cancel() cts.Dispose() cts <- new CancellationTokenSource() override __.Dispose(disposing) = if disposing then cts.Dispose() base.Dispose(true)

我希望有办法避免易变的CancellationTokenSource,但是way.

I wish there was a way to avoid the mutable CancellationTokenSource, but alas.

推荐答案

我可能很想在异步工作流程中编写一个简单的循环.您可以使用do! Async.Sleep interval在两次轮询之间进入睡眠状态-这有两个优点:您不必为了让线程与Thread.Sleep处于空闲状态而捆绑线程,并且如果do!通过了,则do!会自动为您检查取消将CancellationToken转换为Async.Start.

I might be tempted to write a simple loop in an asynchronous workflow. You can use do! Async.Sleep interval to sleep in between polling - this has two advantages: you're not tying up a thread just to have it sit idle with Thread.Sleep, and the do! automatically checks for cancellation for you if you pass a CancellationToken into Async.Start.

此外,如果轮询操作涉及网络通信,则说明您已经处于异步工作流程中,因此进行异步网络呼叫变得很简单.

Plus, if your polling operation involves network communication, you're already in an async workflow, making it trivial to make async network calls.

更多推荐

编写轮询Windows服务

本文发布于:2023-11-14 16:43:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!