1

Topic: Asynchronous loading of the data in Chart

Good afternoon.
Such : there is a window - on it forms user  (Grid, Chart, Button).
In the designer of it  function, which  the data in Chart is caused.
There is it synchronously and not so quickly => the application window hangs up.
From other flow it is impossible: access to an UI-element not from a STA-flow-error.
I tried so:

{
//the Application window - MainWindow
ADC adelc = AddChart;//AddChart - function in which forms 
AsyncCallback cb = new AsyncCallback (MyAsyncCallback);
IAsyncResult ar = adelc. BeginInvoke ("SBER", cb, adelc);
}
public void MyAsyncCallback (IAsyncResult ar)
{
string s;
int iExecThread;
//Because you passed your original delegate in the asyncState parameter
//of the Begin call, you can get it back here to complete the call.
ADC dlgt = (ADC) ar. AsyncState;
//Complete the call.
dlgt. EndInvoke (ar);
}
delegate void ADC (object o);

And besides - the Calling thread should be STA.
If it is simple Dispatcher. BeginInvoke - that is fulfilled not asynchronously.

{
//Window MainWindow
ADC adelc = AddChart;
IAsyncResult ar = adelc. BeginInvoke ("SBER", MyAsyncCallback, "");
}
public void MyAsyncCallback (IAsyncResult ar)
{
MessageBox. Show ("kk");
}
delegate void ADC (object o);

And - a calling thread should be STA.

2

Re: Asynchronous loading of the data in Chart

mraklbrw wrote:

There is it synchronously and not so quickly => the application window hangs up.
From other flow it is impossible: access to an UI-element not from a STA-flow-error.

the Standard approach - to obtain the data asynchronously in DTO (long operation), then quickly to overload in UI-kontroly in flow UI.

3

Re: Asynchronous loading of the data in Chart

Or to use pattern MVVM - to load the representations given in model, and representation itself takes away changes in the necessary flow

4

Re: Asynchronous loading of the data in Chart

The third variant - understand why the data is not ready to the schedule. They should not will be calculated. It is statistics of already last events.

5

Re: Asynchronous loading of the data in Chart

Data is not calculated.
They are added on appropriate areas of the schedule.

6

Re: Asynchronous loading of the data in Chart

What is DTO?

7

Re: Asynchronous loading of the data in Chart

mraklbrw wrote:

the Data is not calculated.
They are added on appropriate areas of the schedule.

the question was why long.
What difference are added or drawn out?

8

Re: Asynchronous loading of the data in Chart

9

Re: Asynchronous loading of the data in Chart

10

Re: Asynchronous loading of the data in Chart

mraklbrw wrote:

function, which  the data in Chart.

it is necessary to divide on

mraklbrw wrote:

function, which

and

mraklbrw wrote:

the data in Chart.

11

Re: Asynchronous loading of the data in Chart

Tried so:

async void VeryLongMethod ()
{
string access = await AddChart ("SBER2");
}
async Task <string> AddChart (object o)
{
//Loading 
}

Also I cause function VeryLongMethod ().
It is fulfilled not asynchronously.

12

Re: Asynchronous loading of the data in Chart

mraklbrw wrote:

Also I cause function VeryLongMethod ().
It is fulfilled not asynchronously.

it is fulfilled asynchronously, but does not wait for the task termination, for async void. You would esteem that-thread about operation with a multithreading.
And as to the task - once again.
(That at you there, loading from a DB, loading from the Internet, etc.) it is necessary to fulfill the DURABLE operation of data acquisition asynchronously (and not through async void) and to add this data in DTO. And also to produce all manipulations over them.
After that in flow UI to appropriate all given to the necessary properties of yours . This operation should be produced quickly, without any calculations, etc. - the data should be already ready.
Then brakes will not be

13

Re: Asynchronous loading of the data in Chart

mraklbrw wrote:

Tried so:

It is a monologue instead of dialogue.
- Why at me 1000 lines are shown quickly in the table. And at you slowly?
Questions do not answer?

14

Re: Asynchronous loading of the data in Chart

mraklbrw wrote:

In the designer of it  function, which  the data in Chart is caused.

  (and generally it is the incorrect approach) let accepts on an input the data, instead of loads their itself. And on good, all data  should receive through properties, and the designer - to be without parameters

15

Re: Asynchronous loading of the data in Chart

In the designer  almost becomes nothing.
Creation  occupies approximately to 3  (too much, yes).
After that function  SetQuotes in which already in Chart candles from already ready HashSet <Candle> are added is caused, i.e. the data is ready - in flow of 15 seconds they (6000 candle approximately) boot in Chart.

candleSeries = new Series ("SeriesCandle")
{
ChartType = SeriesChartType. Candlestick,//Type - Japanese candles
YAxisType = AxisType. Secondary;
ChartArea = "ChartAreaCandle";
ShadowOffset = 2;
YValuesPerPoint = 4
};
_chartForCandle. Series. Add (candleSeries);
for (int i = 0; i <quotes. Count; i ++)
{
//candle Adding
candleSeries. Points. AddXY (i, quotes. ElementAt (i).Low, quotes. ElementAt (i).High, quotes. ElementAt (i).Open, quotes. ElementAt (i).Close);
//signature-time Adding
candleSeries. Points [candleSeries. Points. Count - 1].AxisLabel = quotes. ElementAt (i).Date. ToString (CultureInfo. InvariantCulture);
//Assignment of colors of candles
if (quotes. ElementAt (i).IsPositive) candleSeries. Points [candleSeries. Points. Count - 1].Color = System. Drawing. Color. FromName (Program_Settings. PositiveCandleColor);
else candleSeries. Points [candleSeries. Points. Count - 1].Color = System. Drawing. Color. FromName (Program_Settings. NegativeCandleColor);
}

I.e. here this cycle also occupies a heap of time.

16

Re: Asynchronous loading of the data in Chart

Horror!

mraklbrw wrote:

quotes. ElementAt (i)

  here ElementAt?

17

Re: Asynchronous loading of the data in Chart

HashSet <Candle> - quotes [i] - it is impossible so to address.

18

Re: Asynchronous loading of the data in Chart

mraklbrw wrote:

6000 candle approximately

Give a screen or similar of Google.
On 5 candles on pixel?

19

Re: Asynchronous loading of the data in Chart

Esteem that such Enumerator as works Linq, etc.
At you pass on an array quotes at 6000 elements will be fulfilled 6*6000 times
Replace for on foreach
Immediately!
And never so any more do not do!

20

Re: Asynchronous loading of the data in Chart

mraklbrw wrote:

HashSet <Candle> - quotes [i] - it is impossible so to address.

because it is not necessary to address to it at all.

21

Re: Asynchronous loading of the data in Chart

22

Re: Asynchronous loading of the data in Chart

Further - here it here

mraklbrw wrote:

candleSeries. Points [candleSeries. Points. Count - 1]

  some times? What hinders at first to prepare a point, and then to fulfill AddXY?
And generally, whether it is impossible to prepare an array of points in advance (asynchronously), and then to appropriate it candleSeries. Points? (Here I do not know, whether allows to make it a chart, but it would be logical)

23

Re: Asynchronous loading of the data in Chart

Shocker. Pro wrote:

Esteem that such Enumerator as works Linq, etc.
At you pass on an array quotes at 6000 elements will be fulfilled 6*6000 times
Replace for on foreach
Immediately!
And never so any more do not do!

Corrected, performance of the given code location ~ 1-1.5 seconds
Understood an error, really, it was very silly, I will look further.

24

Re: Asynchronous loading of the data in Chart

mraklbrw wrote:

1-1.5 seconds

Then, I so understood, the topic is solved.
Good luck!

25

Re: Asynchronous loading of the data in Chart

Shocker. Pro wrote:

And generally, whether it is impossible to prepare an array of points in advance (asynchronously), and then to appropriate it candleSeries. Points? (Here I do not know, whether allows to make it a chart, but it would be logical)

But even if does not allow to appropriate an array of points, all the same it is logical to prepare at first an array of points out of the designer, and in the designer it only to add. It what I repeat to you from the very beginning - it is not necessary to obtain the data in the designer, it is necessary to prepare at first them, and then to load in