BeginOnUIThread() public static method

public static BeginOnUIThread ( System.Action action ) : void
action System.Action
return void
コード例 #1
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();
                });
            });
        }
コード例 #2
0
        public void Execute()
        {
            _action.BeginInvoke(
                iar =>
            {
                var result = default(T);
                try
                {
                    result = _action.EndInvoke(iar);
                }
                catch (Exception ex)
                {
                    _errorCallback(ex);
                    return;
                }

                // post the result back on the UI thread
                AsyncHelper.BeginOnUIThread(() => _callback(result));
                AsyncHelper.BeginOnUIThread(() => Completed());
            }, null);
        }