IObservable在Windows服务中订阅和发布
您好,我正在尝试实现一个订阅和发布到 IObservables 的 Windows 服务。
我使用过 http://blogs.tedneward.com/ 2010/11/20/Windows+Service+In+F.aspx 以 Ted Neward 的博客为起点。
type WindowsService() as this
let createAndPublishObservables() =
let obs1 = createObservable1()
let obs2 = createObservable2()
let combObs = Observable.CombineLatest(obs1, obs2) |> map ( fun (firstObsValue, secObsValue) -> firstObsValue + secObsValue)
let obsUpdater = new ObsUpdater()
let updaterPublish = combObs.subscribe( fun x -> obsUpdater.publish(x), ignore, fun () -> ())
override this.OnStart(args:string[])
base.OnStart(args)
// and so on....
当我尝试调试此代码时,似乎服务完成了执行,订阅者和发布者超出了范围(因此反应性消失了)。
我是否需要额外添加一些东西(例如消息泵)才能使其正常工作? (它可以在 Windows 窗体上运行,但到目前为止迁移到服务尚未成功。)
感谢您的帮助!
Hi I am trying to implement a windows service that subscribes and publishes to IObservables.
I have used http://blogs.tedneward.com/2010/11/20/Windows+Service+In+F.aspx from Ted Neward's blog as a starting point.
type WindowsService() as this
let createAndPublishObservables() =
let obs1 = createObservable1()
let obs2 = createObservable2()
let combObs = Observable.CombineLatest(obs1, obs2) |> map ( fun (firstObsValue, secObsValue) -> firstObsValue + secObsValue)
let obsUpdater = new ObsUpdater()
let updaterPublish = combObs.subscribe( fun x -> obsUpdater.publish(x), ignore, fun () -> ())
override this.OnStart(args:string[])
base.OnStart(args)
// and so on....
when I try to debug this code, it seems as if the service finishes its execution and the subscribers and publishers go out of scope (so the reactivity disappears).
Do I need to add something additionally like a message pump to get this to work? (had it working on a windows form but moving to a service has so far not been successful so far.)
Thanks for help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不在 Windows 服务中执行任何操作,它将在 OnStart 后直接退出。您需要保持您的应用程序处于活动状态。执行此操作的常见方法是生成一个单独的线程来无限期地执行处理。然后,在 OnStop 中以某种方式停止线程以使服务终止。
确保该线程不是后台线程。如果是,它不会让应用程序保持活动状态。
当您说您的服务将发布可观察量时,我不确定您的意思。谁将订阅这些可观察数据?
If you don't do anything in your Windows Service, it will exit directly after OnStart. You need to keep your application alive. A common way to do this is to spawn a separate thread that will do your processing indefinitely. Then, stop the thread somehow in OnStop to make the service die.
Make sure that the thread is not a background thread. If it is, it won't keep the app alive.
I'm not sure what you mean when you say that your service will publish observables. Who is going to subscribe to those observables?