Ejemplo n.º 1
0
        async Task <StringBuilder> DoWorkAsync(SqlTester data, CancellationToken token)
        {
            if (token.IsCancellationRequested)
            {
                return(new StringBuilder("cancelled"));
            }

            var task = Task.Run(() =>
            {
                Dispatcher.Invoke(() =>
                {
                    OutputResultControl.AppendText("...working with...." + data.DataBaseType.ToString() + Environment.NewLine);
                });

                if (token.IsCancellationRequested)
                {
                    return(Task.FromResult(new StringBuilder("cancelled")));
                }

                return(data.Execute(token));
            });

            return(await task);
        }
Ejemplo n.º 2
0
        private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (((Button)sender).Content.ToString() == "Cancel")
            {
                GetCanellationtokenForButton((Button)sender).Cancel();
                return;
            }

            var prev = ((Button)sender).Content;

            try
            {
                OutputResultControl.Text = "starting multipple concurrency test" + Environment.NewLine;
                var db_type  = SelectedDBaseType;
                var tst_type = TestType;
                ((Button)sender).Content = "Cancel";
                int concurrentyCount = ConcurrencyCount.GetValueOrDefault(1);

                await Task.Factory.StartNew(() =>
                {
                    try
                    {
                        ParallelOptions po      = new ParallelOptions();
                        CancellationToken token = default;
                        Dispatcher.Invoke(() =>
                        {
                            token = GetCanellationtokenForButton((Button)sender).Token;
                            po.CancellationToken = token;
                        });
                        Parallel.For(0, concurrentyCount, po, async(i, pls) =>
                        {
                            try
                            {
                                var data = new SqlTester(db_type, tst_type);
                                Dispatcher.Invoke(() =>
                                {
                                    OutputResultControl.AppendText("starting test #" + i + " with " + data.DataBaseType.ToString() +
                                                                   Environment.NewLine);
                                });

                                var result = await data.Execute(token);

                                // Display the result. All is ok
                                Dispatcher.Invoke(() =>
                                {
                                    OutputResultControl.Text = result.ToString();
                                });
                            }
                            catch (OperationCanceledException)
                            {
                                pls.Stop();
                                Dispatcher.Invoke(() =>
                                {
                                    OutputResultControl.Text = "cancelled";
                                });
                                return;
                            }
                            catch (Exception ex)
                            {
                                //Show error
                                Dispatcher.Invoke(() =>
                                {
                                    OutputResultControl.Text = "Error: " + ex.Message;
                                });
                            }
                        });
                    }
                    catch (OperationCanceledException)
                    {
                        Dispatcher.Invoke(() =>
                        {
                            OutputResultControl.Text = "cancelled";
                        });
                    }
                    finally
                    {
                        //All sub-task ended. Notify UI
                        Dispatcher.Invoke(() =>
                        {
                            ((Button)sender).Content = prev;
                            var cts = GetCanellationtokenForButton((Button)sender);
                            if (cts.IsCancellationRequested)
                            {
                                cts.Dispose();
                                _cancellationTokenSource.Remove(((Button)sender).Name);
                            }
                        });
                    }
                });
            }
            catch (Exception ex)
            {
                //Show error
                OutputResultControl.Text = "Error: " + ex.Message;
            }
        }