for (int i = 0; i < array.Count; i++) { Thread.Sleep(500); int minValueIndex = i; for (int j = i + 1; j < array.Count; j++) { if (array[j] < array[minValueIndex]) { minValueIndex = j; } } int temp = array[i]; array[i] = array[minValueIndex]; array[minValueIndex] = temp; chart1.Series[0].Points.DataBindXY(null, array); } 

For some reason, the code hangs and only after 10 seconds it builds the already sorted graph, that is, intermediate constructions are not visible. What could it be?

I guess that it affects Thread.Sleep() , but I don’t know how to do without it, because it is necessary to show the sorting step by step [ with intervals between drawing ] ...

  • So you can not do. It is necessary either to cycle with ProcessMessages (as I don’t know in c #) or set a timer, and send a different array once a second - nick_n_a

1 answer 1

You basically stop for half a second (Thread.Sleep) in the main thread, that is, it first considers it in the chart, and the form stream stops on the sorting (the window is not updated, etc.) after that it updates the form, try right after entering a value in the chart call refresh () for it.

  • Indeed, it was enough to add chart1.Update (); after drawing and the result was immediately visible. Thank you very much for your answer! - mr cppst