Search code examples
c#multithreadingbho

How can I access the ui thread in an asynchronous httpwebrequest callback in a bho


I'm struggling to get access to the ui thread in an asynchronous httpwebrequest callback in a browser helper object. The current document and windows don't reflect those witnessed on the calling thread, so I can't update the UI as required.

Can anyone help?

Cheers


Solution

  • I'm not certain what context you're in, but in WinForms, you can access the main Form's UI thread from another thread with form.Invoke() like this:

            // Assuming the following Form and method:
            Form form = ...
            Action method = ...
    
            // Invoke the method like this, so it is run in the UI thread.
            if (form.InvokeRequired)
            {
                form.Invoke(method);
            }
            // If we are already in the UI thread, 
            // just run the method without an invoke.
            else 
            {
                method();
            }