c# - Multithreading with events in WPF -


update: mentioned in comments section problem solved, not understand why way of implementation wrong.

i have situation:

i have device can triggered event in wpf project. event pulls data device @ polling rate of 1ms. want process data in different threads.

my approach was, start backgroundworker registers device event (i read events run on thread called from). in device event data saved object, declared in form. after labels in wpf form refreshed invoke method.

this happens until presses cancel on button in form, unregisters device event , stops thread.

here code use:

declaration in main window:

 public partial class mainwindow : window     {          private backgroundworker worker = new backgroundworker();         private measureobject mobject = new measureobject();     ... } 

this initialization:

public mainwindow()         {             initializecomponent();             this.worker.workersupportscancellation = true;             this.worker.dowork += worker_dowork;             this.worker.runworkercompleted += worker_runworkercompleted;           } 

if button press run background worker:

  private void btnstartmeasure_click(object sender, routedeventargs e)         {              this.worker.runworkerasync();          } 

here register event device. should run on worker thread. tried declare event here too, did not work, placed in main windows.

        private void worker_dowork(object sender, doworkeventargs e)         {              this.mycontroller.controlcenter.diagnostics.newdiagpacketarrived += new eventhandler<newdiagpacketarrivedeventargs>(diagnostics_newdiagpacketarrived);             // run background tasks here         } 

this not needed , empty. worker cancelled if user sets on cancel.

        private void worker_runworkercompleted(object sender,                                                runworkercompletedeventargs e)         {          } 

this event triggered in window , calls 2 functions, should run on backgroundworker if correct.

private void diagnostics_newdiagpacketarrived(object sender, newdiagpacketarrivedeventargs e)         {               try             {                  measure(e);                 this.dispatcher.begininvoke( new action(() => { setstates(e); }),system.windows.threading.dispatcherpriority.input);              }             catch             {              }         }  

measure gets e object device , saves dataobject created

 private void measure(newdiagpacketarrivedeventargs e)         {             lock(this.mobject)             {                 this.mobject.id = this.list.count;                 ....                 this.list.add(this.mobject);               }    } 

setstates refreshed gui

private void setstates(newdiagpacketarrivedeventargs e)         {                lock(this.mobject)             {                  this.lblid.content = this.mobject.id;               }          } 

the problem code if cancel event , thread code:

private void btnstopmeasure_click(object sender, routedeventargs e)         {               this.mycontroller.controlcenter.diagnostics.newdiagpacketarrived -= diagnostics_newdiagpacketarrived;             this.worker.cancelasync();          } 

and try list added objects, objects have same id's , values. seems unregister event or press stop measure button, mobjects in list overwritten mobject @ time when unregister event.

so list looks this:

list[0].id = 1 list[1].id = 1 list[2].id = 1 

rather this:

list[0].id = 1 list[1].id = 2 list[2].id = 3 

maybe can help?

your problem not creating new instance of mobject - create 1 of them here:

private measureobject mobject = new measureobject();

your code adds same object list, , updates that. need make new object each time , put in list.

this.mobject.id = this.list.count;                 ....                 this.list.add(this.mobject); 

currently list list of same object.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -