Skip to content

Latest commit

 

History

History
43 lines (33 loc) · 976 Bytes

VSTHRD004.md

File metadata and controls

43 lines (33 loc) · 976 Bytes

VSTHRD004 Await SwitchToMainThreadAsync

Calls to JoinableTaskFactory.SwitchToMainThreadAsync must be awaited or it is a no-op.

Examples of patterns that are flagged by this analyzer

void MyMethod()
{
    joinableTaskFactory.SwitchToMainThreadAsync();
    UIThreadBoundWork();
}

Solution

Add await in front of the call to JoinableTaskFactory.SwitchToMainThreadAsync.

This requires an async context. Here, we fix the problem by making the outer method async:

async Task MyMethodAsync()
{
    await joinableTaskFactory.SwitchToMainThreadAsync();
    UIThreadBoundWork();
}

Alternatively if found in a synchronous method that cannot be made async, this failure can be fixed by lifting the code into a delegate passed to JoinableTaskFactory.Run:

void MyMethod()
{
    joinableTaskFactory.Run(async delegate
    {
        await joinableTaskFactory.SwitchToMainThreadAsync();
        UIThreadBoundWork();
    });
}