Hello,
Such a problem with this binding - I have been sitting for a week and I cannot understand what I am doing wrong and how it should be done correctly.

Given :
There is an interface, it has 3 graphics.
When you click on one of the menu items:

It should be :

  • During all the work, the GUI is not blocked and the user can stop the test at any time.
  • Change the captions on the graphs.
  • The program sends data to a remote device and receives statuses.
  • When the program receives the correct statuses, it should show the found points on the graphs.
  • After all the necessary data has been sent and all the statuses are received and displayed to the user, the program calculates the parameters for each of the points received. Thereafter:
    - The inscriptions on the graphs change, the graphs are cleared and the calculated parameters (points) appear on these graphs.

What happens :

  • All changes are visible after the last item in "Must be".

How I try to do this:

  1. In the code, created objects (3) within which there is a collection of points ObservableCollection
  2. These three objects are tied to charts via DataContext
  3. In XAML I put SfChart (Syncfusion) charts, I created SplineSeries in them and their ItemSource attached to the collection of the desired object.
  4. In the code, after clicking on the test run, the for loop begins within which data is sent, statuses are obtained, points are needed for this stage, and then these points are displayed on the graph (this does not happen).
  5. As I wrote above, after all this cotovasia, the graphics should change the names and show the calculated parameters.

What am I doing wrong? I do not ask to write code for me, I ask you to explain how to correctly write all these interface changes.
Unfortunately I can not put code samples here (down).
Many thanks to everyone who tries to help!
If the question is incorrectly set - remove.

  • "four. In the code, after clicking on the test run, the for loop begins within which data is sent, statuses are obtained, points are needed for this stage and then these points are displayed on the graph (this does not happen). ”- this is wrong, you are blocking the UI- flow. Upload processing to another thread. - VladD
  • @VladD - How to do this? All cycle is necessary in other flow? - Michael Vaysman
  • Yeah. The whole. Read here: stackoverflow.com/q/615113/10105 (only you have long calculations instead of Sleep ). - VladD
  • You need to run all the logic in a separate thread, so as not to slow down the GUY, - tym32167
  • @Michael: See especially the ParseBigFile example. - VladD

1 answer 1

See it. The problem in clause 4 is “In the code, after clicking on the test run, the for loop begins within which data is sent, statuses are obtained, points for this stage are found, and then these points are displayed on the graph.” Why is it wrong, it is written here: Why does Thread.Sleep behave wrong? How do I make a delay or a long calculation in a graphics program?

Now how to fix it. Your function, working with calculations in a background thread, should deliver results to the main thread. There are many methods to do this, the easiest way, probably through IProgress .

Let's define a data structure describing a new point of the graph:

 class ComputedPoint { public double X, Y; // тут ещё какие-то данные } 

Now our calculation will look like this:

 void Compute(int param1, double param2, IProgress<ComputedPoint> progress) { for (int i = 0; i < param1; i++) { // вычисляете следующую точку progress.Report(new ComputedPoint() { X = 5, Y = computedValue }; } } 

And run it like this:

 Progress<ComputedPoint> p = new Progress<ComputedPoint>(OnNewPoint); await Task.Run(() => Compute(100, 0.5, p)); 

The OnNewPoint method will be called in the necessary flow, the Progress<T> class will take care of this.

In the ComputedPoint method itself, you add a point to the right place:

 void OnNewPoint(ComputedPoint pt) { // тут добавляете точку в коллекцию, к которой у вас привязка } 

Make sure that new points do not come too often, otherwise your application will still hang. If the points are calculated too quickly, transfer them to the UI in batches of several (for example, 100) pieces.