NoWar
NoWar

Reputation: 37633

How to use BeginInvoke method correctly?

I have got this code. It works but it freezes the UI. What I want to know is how to use WPF BeginInvok method corectly?

private void ValidateAuthURL_Click(object sender, RoutedEventArgs e)
{
    ((Button)sender).Dispatcher.BeginInvoke(DispatcherPriority.Input, 
        new ThreadStart(() =>
        {
            bool result = false;
            try
            { 

Upvotes: 0

Views: 369

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500504

Your delegate is going to be executed in the UI thread. That's what Dispatcher.BeginInvoke is there for. I assume you really want to execute that delegate in a background thread... then you should use Dispatcher.BeginInvoke to get back to the UI thread in order to update the UI later.

In terms of getting to a background thread, you could:

  • Use the thread pool directly (ThreadPool.QueueUserWorkItem)
  • Use BackgroundWorker
  • Start a new thread
  • Use Task.Factory.StartNew (if you're using .NET 4)

Upvotes: 3

Related Questions