public void BackgroundProcessing()
        {
            var entryThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            var worker = new BackgroundWorker
            {
                WorkerReportsProgress      = true,
                WorkerSupportsCancellation = true
            };

            worker.DoWork += (sender, e) =>
            {
                var threadInfo = ThreadInfo.GetCurrentThreadInfo();

                for (int i = 1; i <= 100; i++)
                {
                    Thread.Sleep(50);
                    worker.ReportProgress(i);
                }
            };

            worker.ProgressChanged += (sender, e) =>
            {
                Debug.WriteLine(e.ProgressPercentage);
            };

            worker.RunWorkerCompleted += (sender, e) =>
            {
                var threadInfo = ThreadInfo.GetCurrentThreadInfo();
            };

            worker.RunWorkerAsync();
        }
Пример #2
0
        static void Main(string[] args)
        {
            var entryThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            // shows how to block the execution when it's not possible to mark containing method as async
            string result = dotNet45Task.ASyncCallWithContinuation().GetAwaiter().GetResult();

            var returningThreadInfo = ThreadInfo.GetCurrentThreadInfo();
        }
Пример #3
0
        private void button2_Click(object sender, EventArgs e)
        {
            // shows how asynchronous I/O executes using I/O completion ports (watch debug output), but still blocks executing thread

            var entryThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            string result = preTask.AsyncCallWithWait();

            var returningThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            MessageBox.Show(result);
        }
Пример #4
0
        private async void button6_Click(object sender, EventArgs e)
        {
            // shows how using async/await can simplify asynchronous calls (and context synchronization)

            var entryThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            string result = await dotNet45Task.ASyncCallWithContinuation();

            var returningThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            MessageBox.Show(result);
        }
Пример #5
0
        private void button4_Click(object sender, EventArgs e)
        {
            // shows blocking TPM approach to asynchronous calls (no gain over APM)

            var entryThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            string result = dotNet4Task.AsyncCallWithWait();

            var returningThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            MessageBox.Show(result);
        }
Пример #6
0
        private void button5_Click(object sender, EventArgs e)
        {
            // shows non blocking TPM approach to asynchronous calls (but still with callbacks and synchronization problems)

            var entryThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            dotNet4Task.ASyncCallWithContinuation().ContinueWith(task =>
            {
                var returningThreadInfo = ThreadInfo.GetCurrentThreadInfo();

                MessageBox.Show(task.Result);
            }); // fix: }, TaskScheduler.FromCurrentSynchronizationContext());
        }
Пример #7
0
        private void button3_Click(object sender, EventArgs e)
        {
            // shows how asynchronous programming was in APM (callback hell and synchronization problems)

            var entryThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            preTask.ASyncCallWithCallback(result =>
            {
                var returningThreadInfo = ThreadInfo.GetCurrentThreadInfo();

                MessageBox.Show(result);
            });
        }
        public Task <string> BackgroundProcessing()
        {
            return(Task.Factory.StartNew(() =>
            {
                var threadInfo = ThreadInfo.GetCurrentThreadInfo();

                for (int i = 1; i <= 100; i++)
                {
                    Thread.Sleep(50);
                }

                return "ok";
            })); // fix: }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default
        }
Пример #9
0
        private void button7_Click(object sender, EventArgs e)
        {
            // shows how default context synchronization used in conjunction with blocking can cause a deadlock

            // let's assume that we can't change method signature to async so we need to use blocking

            var entryThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            string result = dotNet45Task.ASyncCallWithContinuation().Result; // fix: see DotNet45TaskExamples.ASyncCallWithContinuation() source

            var returningThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            MessageBox.Show(result);
        }
Пример #10
0
        private void button11_Click(object sender, EventArgs e)
        {
            // shows why Task.Factory.StartNew default settings are dangerous

            var entryThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            dotNet4Task.BackgroundProcessing().ContinueWith(task =>
            {
                var returningThreadInfo = ThreadInfo.GetCurrentThreadInfo();

                // second call - watch thread info
                dotNet4Task.BackgroundProcessing(); // fix: see DotNet4TaskExamples.BackgroundProcessing() source

                MessageBox.Show(task.Result);
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }
        public Task <string> BackgroundProcessingAlternative()
        {
            var backgroundTask = new Task <string>(() =>
            {
                var threadInfo = ThreadInfo.GetCurrentThreadInfo();

                for (int i = 1; i <= 100; i++)
                {
                    Thread.Sleep(50);
                }

                return("ok");
            });

            backgroundTask.Start(TaskScheduler.Default);

            return(backgroundTask);
        }
Пример #12
0
        private async void button12_Click(object sender, EventArgs e)
        {
            // shows how to perform ThreadPool calculations in .NET 4.5

            var entryThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            var processingCancellation = new CancellationTokenSource();

            //processingCancellation.Cancel(); // causes TaskCancelledException

            Task <string> processingTask = dotNet45Task.BackgroundProcessing(processingCancellation.Token);

            //Thread.Sleep(2000);
            //processingCancellation.Cancel(); // causes OperationCancelledException

            string result = await processingTask;

            var returningThreadInfo = ThreadInfo.GetCurrentThreadInfo();

            MessageBox.Show(result);
        }
Пример #13
0
        public Task <string> BackgroundProcessing(CancellationToken cancellationToken)
        {
            var progressHandler = new Progress <int>(value =>
            {
                Debug.WriteLine(value);
            });

            var progress = (IProgress <int>)progressHandler;

            return(Task.Run(() =>
            {
                var threadInfo = ThreadInfo.GetCurrentThreadInfo();

                for (int i = 1; i <= 100; i++)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    Thread.Sleep(50);
                    progress.Report(i);
                }

                return "ok";
            }, cancellationToken));
        }