编写轮询Windows服务 [英] Writing a polling Windows service

查看:125
本文介绍了编写轮询Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用C#编写Windows服务,但是我使用F#进行了尝试.对于像这样的轮询服务,我通常使用我编写的类,该类类似于此处 [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服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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