26

Re: Asynchronous loading of the data in Chart

In the designer almost anything is not present.

private void AddChart (string quoteCode)
{
HashSet <Candle> quotes = null;
if (! File. Exists ("Quotes /" + quoteCode + "_M15.txt"))
TimeframesQuotesGenerator.GenerateTimeframeFile (quoteCode, TimeframesQuotesGenerator. Timeframe.m15);
if (quotes == null)
quotes = QuotesStorage. ReadQuotes (quoteCode + "_M15");
ChartGrid chartG1 = new ChartGrid ();
chartG1.QuoteCode = quoteCode;
chartG1.Name = quoteCode + "Chart";
chartG1.Width = Program_Settings. ChartWidth;
chartG1.Height = Program_Settings. ChartHeight;
chartG1.wfh. Width = chartG1.Width;
chartG1.Height = chartG1.Height - 30;
MainGrid. Children. Add (chartG1);
chartG1.SetQuotes (quotes);//M15 quotes
//chartG1.AddIndicator (ChartGrid. LowAreaIndicator. Volume);
//chartG1.AddIndicator (ChartGrid.MainAreaIndicator.MovingAverage);
}

To a call chartG1.SetQuotes (quotes) all is fulfilled almost instantly (though yes, there is a reading of quotations from a file).
But we cannot load the quotation on all accessible tools beforehand in storage?
chartG1.SetQuotes (quotes) - occupies some seconds in aggregate.
Here points on the schedule,  min, max - correction of visual display of the schedule are added.

27

Re: Asynchronous loading of the data in Chart

The designer you do not show, all right, figs with it.
SetQuotes you showed only partially. If in remaining same , as in a case with ElementAt, it is no wonder.
With a high probability, it is possible to prepare a collection of points in advance, out of SetQuotes and out of an UI-stream

28

Re: Asynchronous loading of the data in Chart

And nevertheless, I try to make asynchronous preparation .

void gg ()
{
this. Dispatcher. BeginInvoke (new Action (() => {AddChart ("SBER2");}));
}

I cause:

Thread t1 = new Thread (gg);
t1.Start ();

The window appears quickly, but hangs up, while  function AddChart ().

29

Re: Asynchronous loading of the data in Chart

public void SetQuotes (HashSet <Candle> quotes)
{
this.quotes = quotes;//M15 quotes
Thread t1 = new Thread (QuotesChecker);//Check - whether others are calculated , except M15
t1.Start ();
//It is assigned the data of the main area on the schedule
candleSeries = new Series ("SeriesCandle")
{
ChartType = SeriesChartType. Candlestick,//Type - Japanese candles
YAxisType = AxisType. Secondary;
ChartArea = "ChartAreaCandle";
ShadowOffset = 2;
YValuesPerPoint = 4
};
_chartForCandle. Series. Add (candleSeries);
int counter = 0;
foreach (Candle c in quotes)
{
counter ++;
//candle Adding
candleSeries. Points. AddXY (counter, c. Low, c. High, c. Open, c. Close);
//signature-time Adding
candleSeries. Points [candleSeries. Points. Count - 1].AxisLabel = c. Date. ToString (CultureInfo. InvariantCulture);
//Assignment of colors of candles
if (c. 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);
}
//we Do the cursor on Y red and thick
candleArea. CursorX.LineColor = System. Drawing. Color. Red;
candleArea. CursorX.LineWidth = 2;
candleArea. AxisX.ScaleView. Position = 0;
candleArea. AxisX.ScaleView. Size = 50;
_chartForCandle. ChartAreas [0].AxisX.LabelStyle. Angle =-90;//vertical layout of signatures of dates
_chartForCandle. ChartAreas [0].AxisX.IsLabelAutoFit = false;
_chartForCandle. ChartAreas [0].AxisX.Interval = 5;//the Interval (kol-in candles) between signatures of dates
candleArea. AxisY2.LabelStyle. Format = "#.###";//the Format of the price of a candle
candleArea. AxisY2.IntervalOffset = 1;//That was fixed kol-in inscriptions-prices of candles
int startPozition = 0;//the First displayed candle
int endPozition = candleSeries. Points. Count;//the last displayed candle
GetMinMaxValuesOnChart (startPozition, endPozition, out double min, out double max);
candleArea. AxisY2.Maximum = max;
candleArea. AxisY2.Minimum = min;
ChartResize ();
}
public ChartGrid ()
{
InitializeComponent ();
_chartForCandle = wfh. Child as Chart;//we Receive the schedule from ours 
//Adding of event handlers
_chartForCandle. AxisScrollBarClicked + = chartForCandle_AxisScrollBarClicked;//Click on scroll bar
_chartForCandle. AxisViewChanged + = chartForCandle_AxisViewChanged;//scale Change
_chartForCandle. CursorPositionChanged + = chartForCandle_CursorPositionChanged;//cursor position Change
//It is initialized the main area on the schedule
candleArea = new ChartArea ("ChartAreaCandle")
{
CursorX = {IsUserSelectionEnabled = true, IsUserEnabled = true};
CursorY = {AxisType = AxisType. Secondary};
Position = {Height = 70, X = 0, Width = 100, Y = 0},//a
};
_chartForCandle. ChartAreas. Add (candleArea);//Adding of the main area on the schedule
}

30

Re: Asynchronous loading of the data in Chart

mraklbrw wrote:

And nevertheless, I try to make asynchronous preparation .

the Pancake, learn .
What to sense from Invoke if  the code is fulfilled in an UI-stream? UI thus all the same stops. To you speak as it is necessary, you persistently try to eat a cactus

31

Re: Asynchronous loading of the data in Chart

on a march

mraklbrw wrote:

System. Drawing. Color. FromName ()

what for this operation to fulfill 6000 times?

mraklbrw wrote:

c. Date. ToString (CultureInfo. InvariantCulture);

and it that, it was impossible to prepare in advance in the form of a line, instead of to calculate in an UI-stream?

32

Re: Asynchronous loading of the data in Chart

mraklbrw wrote:

GetMinMaxValuesOnChart

here could not add brakes, I hope?

33

Re: Asynchronous loading of the data in Chart

mraklbrw wrote:

And nevertheless, I try to make asynchronous preparation .

)) The obstinate.
Sticks asynchrony and flows there where they are not necessary.

34

Re: Asynchronous loading of the data in Chart

private void ChartResize ()
{
try
{
if (candleArea == null || candleSeries == null) return;
int startPozition = 0;//the First displayed candle
int endPozition = candleSeries. Points. Count;//the last displayed candle
if (_chartForCandle. ChartAreas. FindByName ("ChartAreaCandle").AxisX.ScrollBar. IsVisible)
{
//If any range is already selected, we assign the first and last proceeding from this range
startPozition = Convert. ToInt32 (candleArea. AxisX.ScaleView. Position);
endPozition = Convert. ToInt32 (candleArea. AxisX.ScaleView. Position) + Convert. ToInt32 (candleArea. AxisX.ScaleView. Size);
}
GetMinMaxValuesOnChart (startPozition, endPozition, out double min, out double max);
candleArea. AxisY2.Maximum = max;
candleArea. AxisY2.Minimum = min;
candleArea. AxisX.ScaleView. Position = startPozition;
candleArea. AxisX.ScaleView. Size = 50;
//Request of update 
if (_chartForCandle. ChartAreas. FindByName ("ChartAreaVolume")! = null) ResizeVolumeIndicator ();
_chartForCandle. Refresh ();
}
catch (Exception ex) {}
}
///<summary>
///Calculation of the maximum and minimum values on the schedule
///</summary>
///<param name = "start"> Nachalnach a candle </param>
///<param name = "end"> the Finite candle </param>
///<param name = "min"> Output parameter - minimum value </param>
///<param name = "max"> Output parameter - the maximum value </param>
private void GetMinMaxValuesOnChart (int start, int end, out double min, out double max)
{
min = double. MaxValue;
max = double. MinValue;
HashSet <Candle> quotes2 = new HashSet <Candle> (quotes. Skip (start).Take (end - start));
foreach (Candle c in quotes2)
{
if (c. High> max) max = c. High;
if (c. Low <min) min = c. Low;
}
}
///<summary>
///Reading of quotations on the given tool from a file
///</summary>
///<param name = "quoteCode"> the tool Code </param>
///<returns> </returns>
public static HashSet <Candle> ReadQuotes (string quoteCode)
{
try
{
if (! File. Exists ("Quotes /" + quoteCode + ".txt") || new FileInfo ("Quotes /" + quoteCode + ".txt").Length <10)
throw new Exception ("the Error: for the given tool of the saved quotations is not present.");
HashSet <Candle> quotes = new HashSet <Candle> ();
using (StreamReader sr1 = new StreamReader ("Quotes /" + quoteCode + ".txt"))
{
while (! sr1.EndOfStream)
{
string tmp = sr1.ReadLine ();
string [] quoteSemblance = tmp. Split (', ');
quotes. Add (new Candle (quoteSemblance [0], quoteSemblance [1], quoteSemblance [2], quoteSemblance [3], quoteSemblance [4], quoteSemblance [5], quoteSemblance [6]));
}
}
return quotes;
}
catch (Exception ex)
{
MessageBox. Show ("a problem at reading of quotations" + quoteCode + ":" + ex. Message);
return new HashSet <Candle> ();
}
}

With calculation of colors corrected, thanks.
At present:
Made the button - at pushing - call AddChart () ~ 2 seconds, of them ~ 300-350ms - loading of quotations from a file.
Check up, please, the code in ReadQuotes ().

35

Re: Asynchronous loading of the data in Chart

mraklbrw wrote:

new HashSet <Candle> (quotes. Skip (start).Take (end - start));

It that for delirium?

36

Re: Asynchronous loading of the data in Chart

///<summary>
///Obtaining of minimum value on the main area of the schedule
///</summary>
private double GetMinValueOnChart (int start, int end)
{
double result = double. MaxValue;
for (int i = start; i <end && i <quotes. Count; i ++)
if (quotes. ElementAt (i).Low <result) result = quotes. ElementAt (i).Low;
return result;
}
///<summary>
///Obtaining of the maximum value on the main area of the schedule
///</summary>
private double GetMaxValueOnChart (int start, int end)
{
double result = 0;
for (int i = start; i <end && i <quotes. Count; i ++)
if (quotes. ElementAt (i).High> result) result = quotes. ElementAt (i).High;
return result;
}

Was earlier so.

37

Re: Asynchronous loading of the data in Chart

At first, in Linq there are aggregate functions, what for them to imitate? However here can and there is a sense to make one pass instead of two.
Secondly, HashSet is not a collection with the determined sequence of elements (wrote that reversal on an index to it is impossible), hence positional operations with it have no logical sense

38

Re: Asynchronous loading of the data in Chart

You did not give class definition Candle, therefore the sense of application  is absolutely not obvious. Explain words, what for it is applied.

39

Re: Asynchronous loading of the data in Chart

[spoiler Candle - here while all is bad with the code]

using System;
using System. Collections. Generic;
using System. Linq;
using System. Text;
using System. Threading. Tasks;
namespace Trade
{
public class Candle
{
public DateTime Date {get;}
public float Open {get;}
public float High {get;}
public float Low {get;}
public float Close {get;}
public int Volume {get;}
public bool IsPositive {get;}//a candle Direction - positive or negative
///<summary>
///the Default constructor
///</summary>
public Candle ()
{
}
///<summary>
///the Designer
///</summary>
///<param name = "quoteString"> the Line with the data-quotations </param>
public Candle (string quoteString)
{
string [] quoteSemblance = quoteString. Split (', ');
Date = new DateTime (Convert. ToInt32 (quoteSemblance [0].Substring (0, 4)), Convert. ToInt32 (quoteSemblance [0].Substring (4, 2)), Convert. ToInt32 (quoteSemblance [0].Substring (6, 2)), Convert. ToInt32 (quoteSemblance [1].Substring (0, 2)), Convert. ToInt32 (quoteSemblance [1].Substring (2, 2)), Convert. ToInt32 (quoteSemblance [1].Substring (4, 2)));
Open = Convert. ToSingle (quoteSemblance [2].Replace ('. ', ', '));
High = Convert. ToSingle (quoteSemblance [3].Replace ('. ', ', '));
Low = Convert. ToSingle (quoteSemblance [4].Replace ('. ', ', '));
Close = Convert. ToSingle (quoteSemblance [5].Replace ('. ', ', '));
Volume = Convert. ToInt32 (quoteSemblance [6]);
IsPositive = Close> Open;
}
///<summary>
///the Designer
///</summary>
///<param name = "date"> Date </param>
///<param name = "open"> discovery Level </param>
///<param name = "high"> Level-maximum </param>
///<param name = "low"> Level-minimum </param>
///<param name = "close"> closing Level </param>
///<param name = "volume"> Volume </param>
public Candle (DateTime date, float open, float high, float low, float close, int volume)
{
Date = date;
Open = open;
High = high;
Low = low;
Close = close;
Volume = volume;
IsPositive = Close> Open;
}
///<summary>
///the Designer
///</summary>
///<param name = "date"> Date </param>
///<param name = "time"> Time </param>
///<param name = "open"> discovery Level </param>
///<param name = "high"> Level-maximum </param>
///<param name = "low"> Level-minimum </param>
///<param name = "close"> closing Level </param>
///<param name = "volume"> Volume </param>
public Candle (string date, string time, string open, string high, string low, string close, string volume)
{
Date = new DateTime (Convert. ToInt32 (date. Substring (0, 4)), Convert. ToInt32 (date. Substring (4, 2)), Convert. ToInt32 (date. Substring (6, 2)), Convert. ToInt32 (time. Substring (0, 2)), Convert. ToInt32 (time. Substring (2, 2)), Convert. ToInt32 (time. Substring (4, 2)));
Open = Convert. ToSingle (open. Replace ('. ', ', '));
High = Convert. ToSingle (high. Replace ('. ', ', '));
Low = Convert. ToSingle (low. Replace ('. ', ', '));
Close = Convert. ToSingle (close. Replace ('. ', ', '));
Volume = Convert. ToInt32 (volume);
IsPositive = Close> Open;
}
public override bool Equals (object obj)
{
if (! (obj is Candle)) throw new Exception ("the Error: attempt of comparing of a candle with object of other type");
Candle c = obj as Candle;
if (this == null || obj == null) return false;
return (this. Date == c. Date && this. Open == c. Open && this. High == c. High && this. Low == c. Low && this. Close == c. Close && this. Volume == c. Volume);
}
public override string ToString ()
{
return GetDate () + "," + GetTime () + "," + this. Open. ToString ().Replace (', ', '. ') + "," + this. High. ToString ().Replace (', ', '. ') + "," + this. Low. ToString ().Replace (', ', '. ') + "," + this. Close. ToString ().Replace (', ', '. ') + "," + this. Volume;
}
public override int GetHashCode ()
{
//return (this. ToString ().GetHashCode ());
return (Date. GetHashCode ());//to Check up - whether HashSet will contain only unique values
}
///<summary>
///Obtaining string  candle dates
///</summary>
///<returns> </returns>
public string GetDate ()
{
string year = Date. Year. ToString ();
string month = Date. Month. ToString ();
if (month. Length == 1) month = "0" + month;
string day = Date. Day. ToString ();
if (day. Length == 1) day = "0" + day;
return year + month + day;
}
///<summary>
///Obtaining string  candle time
///</summary>
///<returns> </returns>
public string GetTime ()
{
string hour = Date. Hour. ToString ();
if (hour. Length == 1) hour = "0" + hour;
string minute = Date. Minute. ToString ();
if (minute. Length == 1) minute = "0" + minute;
string second = Date. Second. ToString ();
if (second. Length == 1) second = "0" + second;
return hour + minute + second;
}
}
}

[/spoiler]
HashSet - situations when any part from the loaded data (it is admissible, percent 5-7-10) can sometimes repeat, therefore are possible that there was no repeating data.
Otherwise each time to check the data read out.

40

Re: Asynchronous loading of the data in Chart

mraklbrw wrote:

can repeat, therefore that there was no repeating data.

well , but because of it you lose a sequence of elements

41

Re: Asynchronous loading of the data in Chart

Yes like on the schedule all is normally deduced.

42

Re: Asynchronous loading of the data in Chart

Well means or the schedule sorts;
Or because of that

public override int GetHashCode ()
{
return (Date. GetHashCode ());
}

the values sorted on , appear in the necessary sequence. But it is impossible so to do.