线程在Windows服务 [英] Threading in a Windows Service

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

问题描述

我已经创建,它使用可观察列出的应用程序。我做了ObservableList类线程(我认为)现在它工作正常在我的应用程序。

I've created an app which uses Observable Lists. I've made the ObservableList class threadsafe (I think) and it's working fine now in my application.

现在我想安装我的应用程序作为服务。这工作得很好,以及,直到达到该点的东西被添加到列表中。我觉得线程那里只是死亡。我有下面的代码:

Now I'm trying to install my application as a service. This works fine as well, up untill the point something gets added to the list. I think the thread there just dies. I've got the following code:

/// <summary>
/// Creates a new empty ObservableList of the provided type. 
/// </summary>
public ObservableList()
{
    //Assign the current Dispatcher (owner of the collection) 
    _currentDispatcher = Dispatcher.CurrentDispatcher;
}

/// <summary>
/// Executes this action in the right thread
/// </summary>
///<param name="action">The action which should be executed</param>
private void DoDispatchedAction(Action action)
{
    if (_currentDispatcher.CheckAccess())
        action.Invoke();
    else
        _currentDispatcher.Invoke(DispatcherPriority.DataBind, action);
}

/// <summary>
/// Handles the event when a collection has changed.
/// </summary>
/// <param name="e"></param>
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
    DoDispatchedAction(() => base.OnCollectionChanged(e));
}



调试时,我已经看到了收藏。添加(对象)被调用。它启动 DoDispatchedAction 功能,并且最后一件事调试器命中,为 _currentDispatcher.Invoke(DispatcherPriority.DataBind,动作); 。在此之后,应用程序将继续但经过中的代码Collection.Add(对象)不会被执行了。最初加入的项目到ObservableList的代码不会继续都不是。这就是为什么我认为线程死亡或者类似的东西。

While debugging, I've seen the Collection.Add(object) being called. It starts the DoDispatchedAction function, and the last thing the debugger hits, is _currentDispatcher.Invoke(DispatcherPriority.DataBind, action);. After this, the application continues but the code after Collection.Add(object) doesn't get executed anymore. The code which initially added the item to an ObservableList doesn't continue neither. That's why I think the Thread dies or something like that.

当检查在调试器的动作,我发现下面的消息在那里:

When checking the action in the debugger, I found out that the following message was there:

的ApartmentState ='_currentDispatcher.Thread.ApartmentState投掷型的
异常'System.Threading.ThreadStateException

ApartmentState = '_currentDispatcher.Thread.ApartmentState' threw an exception of type 'System.Threading.ThreadStateException'

我该如何解决这个问题?我在想,即使在正确的方向?

How can I solve this problem? Am I even thinking in the right direction?

推荐答案

由于这是一个依赖于硬件的服务,这是从通常的LOB式应用程序有点不同。所不同的是:应触发事件的变化来自于应用程序的后端,而整个UI框架和服务架构旨在被使用,使得所述前端要求的量,后端提供数据

As this is a hardware dependent service, this is a little bit different from the usual LOB-style application. The difference is: the changes which should trigger events come from the backend of the application, while the whole UI framework and service architecture is intended to be used so that the frontend asks for data which the backend provides.

您可以通过创建某种中性点接地,他们遇见把两者结合起来。

You could bring the two together by creating some sort of "neutral ground" where they meet.

在硬件处理组件,我会有它运行持续或硬件中断触发运行,并更新其数据结构与它从硬件收集的任何数据后台线程。然后,我将有哪些可以创建在被调用时时间点的硬件数据的一致快照同步的方法。

In the hardware handling component, I would have a background thread which runs continually or runs triggered by hardware interrupts, and updates its data structures with whatever data it collects from the hardware. Then, I would have a synchronized method which can create a consistent snapshot of the hardware data at the point of time when it is called.

在WPF客户端,就是调度定时器,调用设定的时间间隔此方法,并使用更新数据快照ObservableCollections。这是可能的,因为这会在UI线程上发生。其实,如果可能的话,你应该尝试添加并从ObservableCollections删除项目,而不是创造新的集合实例,除非在收集的数据发生变化完全从一个调用到下一个。

In the WPF client, there would be a dispatcher timer which calls this method in set intervals and updates the ObservableCollections using the data snapshots. This is possible, because it would happen on the UI thread. Actually, if possible you should try to add and remove items from the ObservableCollections, not create new collection instances, unless the data in the collection changes completely from one call to the next.

WCF客户端就只能是围绕创建数据快照方法的包装:这将只发送回来时,它被称为这样的快照

The WCF client would only be a wrapper around the method which creates data snapshots: it would only send back such a snapshot when it is called.

在WPF客户端WCF服务将工作作为当地的WPF客户端,只是它会直接调用服务,而不是硬件库,也许我会选择为DispatcherTimer较长的时间间隔,以避免过多的网络流量。可以通过返回一个特殊的代码,这意味着没有任何改变进一步优化这个,为了避免发送相同数据数次,或有不同的方法对询问数据是否已经改变,并检索所述改变后的数据

The WPF client for the WCF service would work as the local WPF client, only it would call the service instead of the hardware library directly, and probably I'd choose a longer interval for the DispatcherTimer, in order to avoid excessive network traffic. You could further optimize this by returning a special code which means "nothing has changed", in order to avoid sending the same data several times, or have separate methods for asking whether data has changed and retrieving the changed data.

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

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