Exemplo n.º 1
0
        private async void Search_Click(object sender, RoutedEventArgs e)
        {
            StockProgress.Visibility      = Visibility.Visible;
            StockProgress.IsIndeterminate = true;
            Search.Content = "Cancel";
            var watch = new Stopwatch();

            watch.Start();
            if (cancellationTokenSource != null)
            {
                cancellationTokenSource.Cancel();
                cancellationTokenSource = null;
                return;
            }

            cancellationTokenSource = new CancellationTokenSource();
            cancellationTokenSource.Token.Register(() =>
            {
                Notes.Text += "Cancellation requested" + Environment.NewLine;
            });

            // var symbols = Ticker.Text.Split(' ');

            try
            {
                await Task.Factory.StartNew(() =>
                {
                    var line = File.ReadAllLines(@"CompanyData.csv");
                    var l    = line
                               .Skip(1)
                               .Select(l => CompanyData.LineToData(l));

                    foreach (var d in l)
                    {
                        Dispatcher.Invoke(() =>
                        {
                            Stocks.Items.Add(d.ToString());
                        });
                    }
                },
                                            CancellationToken.None,
                                            TaskCreationOptions.DenyChildAttach,
                                            TaskScheduler.Default
                                            );
            }
            catch (Exception ex)
            {
                Notes.Text += ex.Message + Environment.NewLine;
            }
            finally {
                cancellationTokenSource = null;
            }

            watch.Stop();
            StocksStatus.Text        = $"Loaded date in {watch.ElapsedMilliseconds}ms";
            StockProgress.Visibility = Visibility.Hidden;
            Search.Content           = "Search";
        }
Exemplo n.º 2
0
        public static Task <IEnumerable <CompanyData> > PollThread()
        {
            var source = new TaskCompletionSource <IEnumerable <CompanyData> >();

            ThreadPool.QueueUserWorkItem(_ => {
                var line = File.ReadAllLines(@"CompanyData.csv");
                var l    = line
                           .Skip(1)
                           .Select(l => CompanyData.LineToData(l));
                source.SetResult(l);
            });
            return(source.Task);
        }
Exemplo n.º 3
0
        private async void Search_Click(object sender, RoutedEventArgs e)
        {
            StockProgress.Visibility      = Visibility.Visible;
            StockProgress.IsIndeterminate = true;
            Search.Content = "Cancel";
            var watch = new Stopwatch();

            watch.Start();
            if (cancellationTokenSource != null)
            {
                cancellationTokenSource.Cancel();
                cancellationTokenSource = null;
                return;
            }

            cancellationTokenSource = new CancellationTokenSource();
            cancellationTokenSource.Token.Register(() =>
            {
                Notes.Text += "Cancellation requested" + Environment.NewLine;
            });

            // var symbols = Ticker.Text.Split(' ');

            try
            {
                CompanyData company = new CompanyData();
                await foreach (var data in company.GetLine(cancellationTokenSource.Token))
                {
                    Stocks.Items.Add(data.ToString());
                }
            }
            catch (Exception ex)
            {
                Notes.Text += ex.Message + Environment.NewLine;
            }

            watch.Stop();
            StocksStatus.Text        = $"Loaded date in {watch.ElapsedMilliseconds}ms";
            StockProgress.Visibility = Visibility.Hidden;
            Search.Content           = "Search";
        }
Exemplo n.º 4
0
        private void NewTask_Click(object sender, RoutedEventArgs e)
        {
            if (cancellationTokenSource != null)
            {
                cancellationTokenSource.Cancel();
                cancellationTokenSource = null;
                return;
            }

            var loadLinesTask = Task.Run(() =>
            {
                var lines = File.ReadAllLines(@"CompanyData.csv");
                return(lines);
            });

            loadLinesTask.ContinueWith(t =>
            {
                var datas = t.Result.Select(l => CompanyData.LineToData(l));

                foreach (var data in datas)
                {
                    Dispatcher.Invoke(() =>
                    {
                        Stocks.Items.Add(data.ToString());
                    });
                }
            }, cancellationTokenSource.Token,
                                       TaskContinuationOptions.OnlyOnRanToCompletion,
                                       TaskScheduler.Current);


            loadLinesTask.ContinueWith(t =>
            {
                Dispatcher.Invoke(() =>
                {
                    Stocks.Items.Add(t.Exception.InnerException.Message);
                });
            });
        }
Exemplo n.º 5
0
        public async IAsyncEnumerable <CompanyData> GetLine(CancellationToken token = default)
        {
            using var stream = new StreamReader(File.OpenRead(@"C:\Users\HP\source\repos\Strumienie\Strumienie\CompanyData.csv"));

            await stream.ReadLineAsync();

            string line;

            while ((line = await stream.ReadLineAsync()) != null)
            {
                if (token.IsCancellationRequested)
                {
                    break;
                }

                var segments = line.Split(',');
                for (var i = 0; i < segments.Length; i++)
                {
                    segments[i] = segments[i].Trim('\'', '"');
                }

                var price = new CompanyData
                {
                    Symbol      = segments[0],
                    CompanyName = segments[1],
                    Exchange    = segments[2],
                    Industry    = segments[3],
                    Website     = segments[4],
                    Description = segments[5],
                    CEO         = segments[6],
                    IssueType   = segments[7],
                    Sector      = segments[8],
                };


                yield return(price);
            }
        }