0

I am having issues on how to get around the following problem.

I have a class which which allows me to dynamically update and display a graph on a form. In order to update the values of the graph I have a method within the class of the form. I pass in the value to update the graph with to this method. Here is a high level example of what I am trying to do:

class GUICLass : Form {

//Code for drawing chart etc all here

public updategraphWithNewValue(double value){

 // Code to update the graph

}

}

My other class is as follows:

class ValueProviderForGraph{

GUIClass graphForm = new GUIClass();

public calculateValuesAndPlot(){

for(int i = 0; i < 4000; i++){

  graphForm.updategraphWithNewValue(i);

  graphForm.update();

}

}

}

Now the issue I get from the above is that the form freezes while this operation is taking place. How could I go about getting around this, any help or advice would be much appreciated. I hope this high level example has enough information, if not please do let me know and I shall try and explain myself better. Thanks.

3 Answers3

5

Look into using BackgroundWorker. Its event-oriented interface should get you up and running quickly.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • BackgroundWorker uses an outdated asynchronous programming model. While it is great for simple cases, I would rather invest time learning TPL. – Eric J. Jan 28 '13 at 22:10
2

You can only update your form's controls from the thread that the control was originally created on.

If you are trying to update it from a different thread, you must marshal that call. There are numerous approaches on the web. My personal favorite (for WinForms) is the following:

https://stackoverflow.com/a/709846/141172

UPDATE

After re-reading your question at the urging of @StenPetrov, I suspect that you do not have a cross-thread issue after all, but that the UI thread is simply not processing messages while it updates the graph (this would cause the freezing during the operation).

If the UI thread is busy updating the graph, you will get that behavior. However, it looks like you are causing the graph to do an unnecessary update 3999 times. Try moving the line

graphForm.update();

outside of your for loop.

While I don't know exactly how your graph control works, I suspect the call to update() causes the entire graph to be re-rendered. You should only do that after all new data points have been added.

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Irrelevant. It would throw an exception if threading was an issue – Sten Petrov Jan 28 '13 at 22:09
  • @StenPetrov: I have experienced a UI freeze in exactly this situation. Perhaps an exception is being thrown and caught somewhere (including in a global exception handler). – Eric J. Jan 28 '13 at 22:10
  • Read OP - "freezes while the operation...", not "hangs" or "crashes" – Sten Petrov Jan 28 '13 at 22:14
  • Hey thanks for you reply :) In response to moving the update to after adding points will rid the graph of its dynamic state and therefore I will see no graph and after all iterations all the points added. This was probably my fault for being very limited in terms of the detail in my question. The for loop above is a simplified version of what I am actually doing which is training and calculating an error within my neural network. I then plot this error to the graph. – user2019718 Jan 29 '13 at 00:42
0

Do you need to update the UI often or not?

If you don't have to update the UI often, such as loading a large graph for viewing, then BackgroundWorker will do.

If you need the UI updated frequently you have to (a) make your graph calculations independent of UI, (b) run the graph calculations in a separate thread and (c) after the update is calculated use Form.Invoke(...) to update the UI

Sten Petrov
  • 10,943
  • 1
  • 41
  • 61