0

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
            { 
2
  • From the example you've posted, you're using it right. However what code is called within the Lambda function? It is possible you have re-entrancy and/or a deadlock - are you using other threads? What is the CPU usage like when frozen. Maximum or zero? Commented Jan 25, 2012 at 11:36
  • @Dr. Andrew Burnett-Thompson I just use var request = WebRequest.Create(serviceUrl) as HttpWebRequest; and etc... Commented Jan 25, 2012 at 11:54

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.