WPF InvokeRequired equivalent

To those who are familiar with the WinForm InvokeRequired, this is will return a boolean true if a UI is being access on a different thread.

the Equivalent WPF for this is:

Declare a delegate function.

        protected delegate void Invoker(string message);


and the actual method that does the UI Update

        public void UpdateUI(string message)
        {
            if (this.richTextBox1.Dispatcher.CheckAccess())
            {
                this.richTextBox1.AppendText(string.Format("{0}: {1}\r", DateTime.Now, message.Message));
            }
            else
            {
                this.richTextBox1.Dispatcher.Invoke(new Invoker(UpdateUI), message);
            }
        }


basically you will just call the UpdateUI on the event handler method.
        protected void StateChange(object sender, StateChangeArgs args)
        {
            UpdateUI(args.Message);
        }


Which is quite simple

Leave a Reply

Proudly powered by Blogger
Theme: Esquire by Matthew Buchanan.
Converted by LiteThemes.com.