ca9163d9
ca9163d9

Reputation: 29179

ConfigureAwait(false) with UI controls?

Is it OK to use .ConfigureAwait(false) for the following two code snippets?

Case 1

var ds = new BindingSource();
ds.DataSource = await CallAsync(); // .ConfigureAwait(false);
UIControl.DataSource = ds;

Case 2

UIControl.DataSource = new BindingSource
{
    DataSource = await CallAsync() // .ConfigureAwait(false)
};

Does the first one seem to have the problem of set UI control at the background thread? How about the second one?

Upvotes: 0

Views: 221

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125237

All access to controls should be done in the same thread which the control is created.

By calling ConfigureAwait(false) you are asking not to attempt to marshal the continuation back to the original context captured. It means the code continue the execution in a different context than the UI thread which is invalid operation.

So, the answer is yes, both above cases have problem and will result in:

InvalidOperationException: Cross-thread operation not valid: Control 'Control Name' accessed from a thread other than the thread it was created on.

Upvotes: 2

Related Questions