Question

I am new to WPF.

CODE 1:

      if (!Dispatcher.CheckAccess())
            {

                if (ab == "abc")
                {
                    Dispatcher.Invoke( () =>
                    lab1.Visibility = Visibility.Visible

                    , DispatcherPriority.Normal);

                }
            }

here in the above code i just want to change the visibility on Label.

But with multiple changes like this ....

CODE 2 :

      if (!Dispatcher.CheckAccess())
            {

                if (ab == "abc")
                {
                    Dispatcher.Invoke( () =>
                    lab1.Visibility = Visibility.Visible

                    lab2.Visibility = Visibility.Hidden

                    lab3.Visibility = Visibility.Hidden

                    , DispatcherPriority.Normal);

                }
            }

its not working , so Can anyone help me out how to Invoke multiple instances ?

Was it helpful?

Solution

I expect your code isn't compiling. You should change your dispatcher todo this:

Dispatcher.Invoke( new Action( () =>
     {
         lab1.Visibility = Visibility.Visible;
         lab2.Visibility = Visibility.Hidden;
         lab3.Visibility = Visibility.Hidden;
     } ) );

The Dispatcher is used to dispatch items from different threads onto the UI thread. CheckAccess() will check to see if you are on the Dispatcher thread (or UI thread). If you already are, it will not go into your if statement. Are you absolutely sure that you already aren't on the Dispatcher thread?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top