Example #1
0
        /// <summary>
        /// This call will block the UI thread... it also shows off the C# yield statement
        /// </summary>
        private void btnDoWorkNormal_Click(object sender, EventArgs e)
        {
            txtLog.Clear();

            var watch = new Stopwatch();

            watch.Start();

            // let user know we're working...
            statusLabel.Text = "Processing payment...";

            // call slow API
            var chargeResults = SlowApiCall.ProcessPayment();

            // update UI with results
            txtLog.AppendText($"Process Payment Results: {chargeResults}{Environment.NewLine}");
            statusLabel.Text = "Saving to database...";

            // call database with results from API
            var dbResults = SlowDatabase.LogCharge(chargeResults);

            // update UI
            txtLog.AppendText($"Database Results: {dbResults}{Environment.NewLine}");

            statusLabel.Text = "DONE!";
            txtLog.AppendText($"Done!{Environment.NewLine}{Environment.NewLine}");

            watch.Stop();

            txtTotalTime.Text = watch.Elapsed.ToString();
        }
Example #2
0
        /// <summary>
        /// Example of using async/await to add threading (non-blocking) to our app.
        ///
        /// Can you see how these are basically doing the same thing?
        /// </summary>
        private async Task DoWorkWithAsync()
        {
            txtLog.Clear();

            var watch = new Stopwatch();

            watch.Start();

            // let user know we're working...
            statusLabel.Text = "Processing payment...";

            // call slow API
            var chargeResults = await Task.Run(() => SlowApiCall.ProcessPayment());

            // update UI with results
            txtLog.AppendText($"Process Payment Results: {chargeResults}{Environment.NewLine}");

            // let user know we're saving to the database
            statusLabel.Text = "Saving to database...";

            // call database with results from API
            var dbResults = await Task.Run(() => SlowDatabase.LogCharge(chargeResults));

            // update UI with results
            txtLog.AppendText($"Database Results: {dbResults}{Environment.NewLine}");

            statusLabel.Text = "DONE!";
            txtLog.AppendText($"Done!{Environment.NewLine}{Environment.NewLine}");

            watch.Stop();

            txtTotalTime.Text = watch.Elapsed.ToString();
        }
Example #3
0
        private void btnNonBlockedUIManual_Click(object sender, EventArgs e)
        {
            txtLog.Clear();

            var watch = new Stopwatch();

            watch.Start();

            // let user know we're working...
            statusLabel.Text = "Processing payment...";

            // call slow API
            Task.Run(() => SlowApiCall.ProcessPayment())
            .ContinueWith(prevTask =>
            {
                var chargeResults = prevTask.Result;

                AsyncHelper.BeginOnUIThread(() =>
                {
                    // update UI with results
                    txtLog.AppendText($"Process Payment Results: {chargeResults}{Environment.NewLine}");

                    // let user know we're working
                    statusLabel.Text = "Saving to database...";
                });

                // call database with results from API
                return(SlowDatabase.LogCharge(chargeResults));
            }).ContinueWith(prevTask =>
            {
                // call database with results from API
                var dbResults = prevTask.Result;

                AsyncHelper.BeginOnUIThread(() =>
                {
                    // update UI
                    txtLog.AppendText($"Database Results: {dbResults}{Environment.NewLine}");

                    statusLabel.Text = "DONE!";
                    txtLog.AppendText($"Done!{Environment.NewLine}{Environment.NewLine}");

                    watch.Stop();

                    txtTotalTime.Text = watch.Elapsed.ToString();
                });
            });
        }