c# - Synchronizing sub-thread with main thread - not so obvious problem -


Honestly, I'm a little lame when it comes to threading;) Then I'm asking a little help.

Suppose we have some kind of control on which we can draw some charts. There is also a method that pulls the chart on this control. The problem is that the charting system is only for one of the areas of control and when the chart is ready, we need to refresh the control.

So assume that our control looks like this:

  Class Charting Control: System. Windows .form. Control {public canvas canvas; Public Zero Refresh (); / * ... other fields / methods * /}  

where canvas is a class to attract the image (something like graphics).
Charting method is used only for canvas objects (we can not change this), so it looks like this:

  Public stateless draw chart (Canvas) {/ * Draw This method can be called from a different thread, background worker etc ... and I have to synchronize it with the main thread and call  refresh ()  when the chart is ready it happens. 

Now when the chart is ready, then I set a flag on the canvas object

  public status blank draft drawer (canvas) {/ * draw * / canvas .Tag = true; // chart is ready}  

and I have a background worker running inside the charting control and if the canvas.Tag field has changed and is listening , Call this refresh ()

but it seems that my method is a little less, easy-fail etc. ... is there any better way to improve it?

Limitations:
- We can not modify the canvas category. What we can use is the tag field (type object
- we can modify the chartingcontroll class and drawing method
- There may be many charting controls - we do not have any control over how called Draw Chart . It can be said in a separate thread, or not. It is said to build control and drawarty method and earn them somehow. Shish

Solution
OK, I sorted it out like this: Charting Control I created a manual reset event manual reset < / Code> and background worker.

Bacground worker manual reset :

  is waiting for zero background Worker_DoWork (object sender , System.ComponentModel.DoWorkEventArgs e) {manualReset.WaitOne (); // Wait for a chart to be completed}  

and finally call the reset method () Switch to

  void Prishtbhumivrkr_runwack full (object sender, System.Co Monpenntmodel. Runwalkeruplicated avantarges e) {refresh (); }  

I place the manual code inside canvas tags and drawing method, when the chart is ready, then I Reset the manual reset .set ();

OK, I like it to pass the signal to prepare the chart Resolved: In the Charting Control I Manual ResetEvent manualReset and background worker.

ManualReset.WaitOne (); // Wait for a chart to be completed}

and finally reset () method

  zero background worker_recognized full (object sender, System.ComponentModel call ) RunWorkerCompletedEventArgs e) {refresh (); }  

I place the manual code inside canvas tags and drawing method, when the chart is ready, then I Reset the manual reset .set (); to pass the signal to prepare the chart


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -