Dynamic login display state of FIG wpf

In WPF, a DispatcherObject can only be accessed by the Dispatcher it is associated with. For example, a background thread cannot update the contents of a Button that is associated with the Dispatcher on the UI thread. In order for the background thread to access the Content property of the Button, the background thread must delegate the work to the Dispatcher associated with the UI thread. This is accomplished by using either Invoke or BeginInvokeInvoke is synchronous and BeginInvoke is asynchronous. The operation is added to the queue of the Dispatcher at the specified DispatcherPriority.

If BeginInvoke is called on a Dispatcher that has shut down, the status property of the returned DispatcherOperation is set to Aborted.

All of the methods on Dispatcher, with the exception of DisableProcessing, are free-threaded.

Objects that derive from DispatcherObject have thread affinity.

The following example demonstrates how when the login process, status icons displayed on the screen, after logging in complete hiding icons:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Thread th1 = new Thread(Method1);            
            th1.Start();

            // show progressbar
            progressBar.Visibility = Visibility.Visible;
        }

        void Method1()
        {
            MessageBox.Show("starting");  

            // process your work...
            Thread.Sleep(5000);

            MessageBox.Show("ending");

            // hide progressbar
            this.Dispatcher.BeginInvoke(new Action(() => { progressBar.Visibility = Visibility.Collapsed; }));
        }
    }

Related Resources: https: //docs.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatcher view = netframework-4.8?

Guess you like

Origin www.cnblogs.com/hellowzl/p/11229812.html