.

Dispatcher.BeginInvoke (invokeRequired equivalent for WPF)

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

        private void button_Click(object sender, RoutedEventArgs e)
        {
            Timer t = new Timer(1000);
            t.Elapsed += new ElapsedEventHandler(t_Elapsed);
            t.Start();
        }

        void TimerAction(object sender, EventArgs e)
        {
            if (image1.Dispatcher.Thread == System.Threading.Thread.CurrentThread)
            {
                image1.Visibility = (image1.Visibility == System.Windows.Visibility.Hidden ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden);
            }
            else
            {
                image1.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new EventHandler<EventArgs>(TimerAction), sender, new object[] { e });
            }
        }

        void t_Elapsed(object sender, ElapsedEventArgs e)
        {
            TimerAction(null, null);
        }

    }

One Response to this post.

  1. Posted by Pietman on 15.06.12 at 2:28 pm

    also:
    if (someControl.Dispatcher.CheckAccess())
    UpdateUI();
    else
    someControl.Dispatcher.Invoke(new Action(UpdateUI));

What's your thoughts on this?

*

Protected by WP Anti Spam