Example #1
0
        /// <summary>
        /// background 'open file' thread (callbacks inside)
        /// </summary>
        /// <param name="filename"></param>
        void OpenFileBackground(string filename)
        {
            ProgressWindow progressWindow = new ProgressWindow("In progress", $"Opening{filename}", _openBackground, statusStrip1);
            Task           r        = Task.Run(delegate() { Application.Run(progressWindow); });
            double         fileSize = new FileInfo(filename).Length;
            double         current  = 0;
            double         previousPercentForRefreshGraph = 0;
            double         prevPercentForCallback         = 0;
            double         percent = 0;

            if (_FieldCeparatorMode == fieldCeparatorMode.auto)
            {
                //predicting field separator
                using (StreamReader reader = new StreamReader(filename))
                {
                    string line = reader.ReadLine();
                    if (line.Contains("\t"))
                    {
                        _fieldSeparator = "\t";
                    }
                    else if (line.Contains(";"))
                    {
                        _fieldSeparator = ";";
                    }
                    else
                    {
                        _fieldSeparator = ",";
                    }
                }
            }
            if (_DecimalSeparatorMode == decimalCeparatorMode.auto)
            {
                //predicting decimal separator
                using (StreamReader reader = new StreamReader(filename))
                {
                    if (_fieldSeparator == ",")
                    {
                        _decimalSeparator = ".";
                    }
                    else
                    {
                        //first line
                        reader.ReadLine();

                        string line = reader.ReadLine();
                        while (!line.Contains(".") && !line.Contains(","))
                        {
                            line = reader.ReadLine();
                        }
                        if (line.Contains("."))
                        {
                            _decimalSeparator = ".";
                        }
                        else if (line.Contains(","))
                        {
                            _decimalSeparator = ",";
                        }
                    }
                }
            }

            using (StreamReader reader = new StreamReader(filename))
            {
                //Get Names
                string   line = reader.ReadLine();
                string[] row  = line.Split(new string[] { _fieldSeparator }, StringSplitOptions.RemoveEmptyEntries);
                float[]  floatRow;
                current = line.Length;
                int channelCount = row.Length;
                floatRow = new float[channelCount];
                listBox1.Invoke((MethodInvoker) delegate()
                {
                    listBox1.SelectedIndexChanged -= listBox1_SelectedIndexChanged;
                    listBox1.Items.Clear();

                    for (int i = 0; i < row.Length; i++)
                    {
                        graph2.addChannel(row[i]);
                        listBox1.Items.Add(row[i]);
                        if (i < 5)
                        {
                            listBox1.SelectedIndices.Add(i);
                        }
                    }
                    listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;
                });
                //get values async
                while (!reader.EndOfStream)
                {
                    line = reader.ReadLine();
                    if (Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator != _decimalSeparator)
                    {
                        line = line.Replace(_decimalSeparator, Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator);
                    }
                    row = line.Split(new string[] { _fieldSeparator }, StringSplitOptions.None);
                    if (row.Length == channelCount)
                    {
                        for (int i = 0; i < channelCount; i++)
                        {
                            if (!float.TryParse(row[i], out floatRow[i]))
                            {
                                floatRow[i] = float.NaN;
                            }
                        }
                        if (!float.IsNaN(floatRow[0]))
                        {
                            for (int i = 0; i < channelCount; i++)
                            {
                                graph2.insertInto(i, new PointF(floatRow[0], floatRow[i]));
                            }
                        }
                    }
                    current += line.Length;
                    percent  = (int)(current / fileSize * 100);
                    if (percent != prevPercentForCallback)
                    {
                        prevPercentForCallback = percent;
                        if (progressWindow.IsHandleCreated)
                        {
                            progressWindow.BeginInvoke((MethodInvoker) delegate()
                            {
                                if (progressWindow.IsHandleCreated)
                                {
                                    progressWindow.UpdateProgressBar(percent);
                                }
                            });
                        }
                        if (percent - previousPercentForRefreshGraph > 5)
                        {
                            graph2.BeginInvoke((MethodInvoker) delegate()
                            {
                                try
                                {
                                    if (graph2.IsHandleCreated)
                                    {
                                        graph2.draw(false);
                                    }
                                }
                                catch (Exception ex) { }
                            });
                            statisticBox.BeginInvoke((MethodInvoker) delegate()
                            {
                                statisticBox.Text  = "";
                                List <string> stat = graph2.GetStatistic();
                                stat.Insert(0, $"File: {filename}");
                                statisticBox.Lines = stat.ToArray();
                            });
                            previousPercentForRefreshGraph = percent;
                        }
                    }
                }
            }
            if (graph2.IsHandleCreated)
            {
                graph2.BeginInvoke((MethodInvoker) delegate()
                {
                    try
                    {
                        if (graph2.IsHandleCreated)
                        {
                            graph2.draw(true);
                        }
                    }
                    catch (Exception ex) { }
                });
            }
            statisticBox.BeginInvoke((MethodInvoker) delegate()
            {
                statisticBox.Text  = "";
                List <string> stat = graph2.GetStatistic();
                stat.Insert(0, $"File: {filename}");
                statisticBox.Lines = stat.ToArray();
            });
            if (progressWindow.IsHandleCreated)
            {
                progressWindow.BeginInvoke((MethodInvoker) delegate() {
                    if (progressWindow.IsHandleCreated)
                    {
                        progressWindow.UpdateProgressBar(100);
                    }
                });
            }
        }
Example #2
0
        /// <summary>
        /// background 'generate file' thread (callbacks inside)
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="channelCount"></param>
        /// <param name="rowCount"></param>
        /// <param name="NaNs"></param>
        void GenerateFileBackground(string filename, int channelCount, int rowCount, int NaNs = -1)
        {
            //file generator
            ProgressWindow progressWindow = new ProgressWindow("In Process", $"Generating CSV File '{filename}'", Thread.CurrentThread, statusStrip1);


            Task r = Task.Run(delegate() { Application.Run(progressWindow); });

            List <Generator>      generators = new List <Generator>();
            Random                random     = new Random();
            List <List <double> > values     = new List <List <double> >();

            for (int i = 0; i < channelCount; i++)
            {
                int classType = random.Next(0, 3);
                switch (classType)
                {
                case 0:
                    generators.Add(new Sin(random.Next(1, 100) / (10 * 1.0f)));
                    break;

                case 1:
                    generators.Add(new Cos(random.Next(1, 100) / (10 * 1.0f)));
                    break;

                case 2:
                    generators.Add(new SinCos(random.Next(1, 100) / (10 * 1.0f)));
                    break;
                }
            }
            float         step = 0.01F;
            List <string> line = new List <string>();

            for (int i = 0; i < channelCount + 1; i++)
            {
                line.Add(i.ToString());
            }
            NumberFormatInfo nfi = new NumberFormatInfo();

            nfi.NumberDecimalSeparator = _decimalSeparator;
            using (StreamWriter writer = new StreamWriter(filename))
            {
                writer.WriteLine(String.Join(_fieldSeparator, line));
                for (float i = 0, j = 0; j < rowCount; i += step, j++)
                {
                    line = new List <string>();
                    line.Add(i.ToString());
                    foreach (var generator in generators)
                    {
                        if (random.Next(0, 100) < NaNs)
                        {
                            line.Add("err");
                        }
                        else
                        {
                            line.Add(generator.getValue(i).ToString(nfi));
                        }
                    }
                    writer.WriteLine(String.Join(_fieldSeparator, line));
                    if (progressWindow.IsHandleCreated)
                    {
                        progressWindow.BeginInvoke((MethodInvoker) delegate()
                        {
                            if (progressWindow.IsHandleCreated)
                            {
                                progressWindow.UpdateProgressBar(Math.Round(j / (rowCount * 1.0f) * 100, 3));
                            }
                        });
                    }
                }
            }
            if (progressWindow.IsHandleCreated)
            {
                progressWindow.BeginInvoke((MethodInvoker) delegate()
                {
                    if (progressWindow.IsHandleCreated)
                    {
                        progressWindow.UpdateProgressBar(100);
                    }
                });
            }
        }