Beispiel #1
0
        private async void btnStartAnonum_Click(object sender, EventArgs e)
        {
            btnStartAnonum.Text = "Cancel";

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

            cancellationTokenSource = new CancellationTokenSource();
            try
            {
                var service   = new StockService();
                var operation = Task.Factory.StartNew(async(obj) => // obj is the service
                {
                    var stockService = obj as StockService;

                    var prices = await stockService.SearchForStocks("MSFT", cancellationTokenSource.Token);

                    return(prices.Take(5));
                }, service)// pass as obj into lambda
                                .Unwrap();

                //var result = await await operation;// without Unwrap()
                var result = await operation;

                Debug.WriteLine("Process completed");
            }
            catch (Exception ex)
            {
                textBox1.Text = ex.Message;
            }
            finally
            {
                cancellationTokenSource = null;
            }

            btnStartAnonum.Text = "19 StartAnonum";
        }
Beispiel #2
0
        private async void btnOneByOne_Click(object sender, EventArgs e)
        {
            btnOneByOne.Text = "Cancel";

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

            cancellationTokenSource = new CancellationTokenSource();

            cancellationTokenSource.Token.Register(() => { textBox1.Text = "Cancellation requested"; });

            try
            {
                var tickers            = tbTickers.Text.Split(',', ' ');
                var tickerLoadingTasks = new List <Task <IEnumerable <StockPrice> > >();
                var service            = new StockService();
                var stocks             = new ConcurrentBag <StockPrice>();//thread safe collection

                foreach (var item in tickers)
                {
                    var loadedLines = service.SearchForStocks(item, cancellationTokenSource.Token)
                                      .ContinueWith(t =>
                    {
                        foreach (var stock in t.Result.Take(5))
                        {
                            stocks.Add(stock);
                        }

                        GuiDelegate del = delegate
                        {
                            dataGridView1.DataSource       = stocks.Select(p => new { value = $"{p.Ticker} {p.Volume}" }).ToList();
                            dataGridView1.Columns[0].Width = dataGridView1.Width;
                        };

                        dataGridView1.Invoke(del);

                        return(t.Result);
                    });

                    tickerLoadingTasks.Add(loadedLines);
                }

                var allLoadingTasks = Task.WhenAll(tickerLoadingTasks);

                await allLoadingTasks;
            }
            catch (Exception ex)
            {
                textBox1.Text = ex.Message;
            }
            finally
            {
                cancellationTokenSource = null;
            }

            btnOneByOne.Text = "9 One By One";
        }