Wednesday, April 7, 2010

How to use the dispatcher to run a new thread and how to make calls back to ProgressBar from Dispatcher

I got this technique from this link:
http://windowsclient.net/learn/video.aspx?v=57027

Its related to using the BackgroundWorker, apparently BackgroundWorker calles Dispatcher, but this example is so simple that I like it.

        private void DoSomething()
        {
                    new Thread(
                      delegate()
                      {
                        Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { progressBar.IsIndeterminate = true; }, null);
                        LongRunningProcess();
                        Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { progressBar.IsIndeterminate = false; }, null);
                      }
                    ).Start();
        }

No comments: