inovke和begininovke在Windows服务中 [英] inovke and begininovke in windows services

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

问题描述

我的Windows窗体中有功能,现在我正在尝试将我的工作转移到Windows服务,转移后我注意到Windows服务不支持InvokeRequired和BeginInvoke

I have function in my Windows Form and now I'm trying to transfer my work to windows service, After transferring I notice that InvokeRequired and BeginInvoke are not supported by windows service

推荐答案

你必须谈论 System.Windows.Forms.Control.Invoke System.Windows.Forms.Control.Invoke 。这些方法特定于此表单库,与Windows服务无关。委托调用Form UI线程的任何委托实例。我在过去的答案中解释了它们:

控制。 Invoke()vs. Control.BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ]。



服务没有UI线程,所以这个东西完全是不适用于它。如果由于某种原因,您需要将某个委托实例调用委托给某个不同的线程,则应该适当地设计此线程以接受来自某个队列的那些调用请求。您可以从我的文章中了解它的工作原理:

用于线程通信和线程间调用的简单阻塞队列 [ ^ ]。



我必须说,至少在你工作的某些部分你拿错误的路线。您不应该尝试将某个应用程序从一种应用程序类型转换为另一种应用程序类型,特别是如果您不清楚某些技术或类似此调用机制的内容。相反,您应该从方面将原始产品分解为多个部分。首先,您需要隔离UI,从不幸存的部分从一种应用程序类型移植到另一种应用程序类型。 (在您的情况下,Windows服务根本没有UI。)实际上,在创建此产品时,首先需要将UI与产品的其他方面彻底分离,而不是在移植时。



-SA
You must be talking about System.Windows.Forms.Control.Invoke and System.Windows.Forms.Control.Invoke. These methods are specific to this Forms library and have nothing to do with Windows Services. The delegate the invocation of any delegate instances the Form UI thread. I explained them in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

A Service does not have a UI thread, so this stuff it totally inapplicable to it. If, by some reason, you would need to delegate some delegate instance invocation to some different thread, this thread should be appropriately designed to accept those invocation request from some queue. You can learn how it work from my article:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

I must say, at least in some part of your work you take a wrong route. You should not try "translate" some application from one application type to another application type, especially if you don't clearly understand some techniques or things like this invocation mechanism. Instead, you should start from decomposing the original product into parts by aspects. First of all, you need to isolate UI, the part with never "survives" porting from one application type to another. (In your case, Windows Services don't have UI at all.) Actually, you need to thoroughly separate UI from other aspects of you product in first place, when you create this product, not when you port it.

—SA


这篇关于inovke和begininovke在Windows服务中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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