Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeginGetResponse from BackgroundWorker

I am creating a webrequest both from the UI thread and a BackgroundWorker thread.

IAsyncResult result = request.BeginGetResponse (o => {
    callCallback (o);
}, state);

....

private void callCallback (IAsyncResult o)
{
    RequestStateGen state = (RequestStateGen)o.AsyncState;
    state.context.Post (_ => {
        onGetCustomListObject (o);
    }, null);
}

When I call this code from the UI thread it works fine. When called from the BackgroundWorker thread it freezes on BeginGetResponse.

The UI is still responsive. Any thoughts?

Edit: I figured out that I actually not need the UI context when calling from my background thread. And I think it would've been really hard injecting a ui context that always is valid. Here is my code now that works:

private void callCallback (IAsyncResult o)
{
    RequestStateGen state = (RequestStateGen)o.AsyncState;
    if (!state.OnBgThread)
        {
            state.context.Post (_ => {
                onGetCustomListObject (o);
            }, null);
        }
        else
        {
            onGetCustomListObject (o);
        }
    }
}
like image 854
Sunkas Avatar asked Jul 29 '26 17:07

Sunkas


1 Answers

Your code is reliant on SynchronizationContext.Current grabbing the UI context. If you're already in a background thread then that context will not be the UI's context.

You can call BeginGetResponse from a background thread just fine, but you need to ensure that SynchronizationContext.Current is called from the UI thread and somehow exposed to your background thread.

like image 55
Servy Avatar answered Aug 01 '26 06:08

Servy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!