JoinableTaskContext::SuppressRelevance Method ()

 

Conceals any ticket to the Main thread until the returned value is disposed.

Namespace:   Microsoft.VisualStudio.Threading
Assembly:  Microsoft.VisualStudio.Threading (in Microsoft.VisualStudio.Threading.dll)

public:
JoinableTaskContext::RevertRelevance SuppressRelevance()

In some cases asynchronous work may be spun off inside a delegate supplied to Run so that the work does not have privileges to re-enter the Main thread until the Run call has returned and the UI thread is idle. To prevent the asynchronous work from automatically being allowed to re-enter the Main thread, wrap the code that calls the asynchronous task in a using block with a call to this method as the expression.

this.JobContext.RunSynchronously(async delegate {
    using(this.JobContext.SuppressRelevance()) {
        var asyncOperation = Task.Run(async delegate {
            // Some background work.
            await this.JobContext.SwitchToMainThreadAsync();
            // Some Main thread work, that cannot begin until the outer RunSynchronously call has returned.
        });
    }

    // Because the asyncOperation is not related to this Main thread work (it was suppressed),
    // the following await *would* deadlock if it were uncommented.
    ////await asyncOperation;
});
Return to top
Show: