c# - Threading in a Windows Service -


i've created app uses observable lists. i've made observablelist class threadsafe (i think) , it's working fine in application.

now i'm trying install application service. works fine well, untill point gets added list. think thread there dies. i've got following code:

/// <summary> /// creates new empty observablelist of provided type.  /// </summary> public observablelist() {     //assign current dispatcher (owner of collection)      _currentdispatcher = dispatcher.currentdispatcher; }  /// <summary> /// executes action in right thread /// </summary> ///<param name="action">the action should executed</param> private void dodispatchedaction(action action) {     if (_currentdispatcher.checkaccess())         action.invoke();     else         _currentdispatcher.invoke(dispatcherpriority.databind, action); }  /// <summary> /// handles event when collection has changed. /// </summary> /// <param name="e"></param> protected override void oncollectionchanged(notifycollectionchangedeventargs e) {     dodispatchedaction(() => base.oncollectionchanged(e)); } 

while debugging, i've seen collection.add(object) being called. starts dodispatchedaction function, , last thing debugger hits, _currentdispatcher.invoke(dispatcherpriority.databind, action);. after this, application continues code after collection.add(object) doesn't executed anymore. code added item observablelist doesn't continue neither. that's why think thread dies or that.

when checking action in debugger, found out following message there:

apartmentstate = '_currentdispatcher.thread.apartmentstate' threw exception of type 'system.threading.threadstateexception'

how can solve problem? thinking in right direction?

as hardware dependent service, little bit different usual lob-style application. difference is: changes should trigger events come backend of application, while whole ui framework , service architecture intended used frontend asks data backend provides.

you bring 2 creating sort of "neutral ground" meet.

in hardware handling component, have background thread runs continually or runs triggered hardware interrupts, , updates data structures whatever data collects hardware. then, have synchronized method can create consistent snapshot of hardware data @ point of time when called.

in wpf client, there dispatcher timer calls method in set intervals , updates observablecollections using data snapshots. possible, because happen on ui thread. actually, if possible should try add , remove items observablecollections, not create new collection instances, unless data in collection changes 1 call next.

the wcf client wrapper around method creates data snapshots: send such snapshot when called.

the wpf client wcf service work local wpf client, call service instead of hardware library directly, , i'd choose longer interval dispatchertimer, in order avoid excessive network traffic. further optimize returning special code means "nothing has changed", in order avoid sending same data several times, or have separate methods asking whether data has changed , retrieving changed data.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -