Bagikan melalui


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)

Syntax

'Declaration
Public Function SuppressRelevance As JoinableTaskContext.RevertRelevance
public JoinableTaskContext.RevertRelevance SuppressRelevance()
public:
JoinableTaskContext::RevertRelevance SuppressRelevance()
member SuppressRelevance : unit -> JoinableTaskContext.RevertRelevance
public function SuppressRelevance() : JoinableTaskContext.RevertRelevance

Return Value

Type: Microsoft.VisualStudio.Threading.JoinableTaskContext.RevertRelevance
Returns JoinableTaskContext.RevertRelevance.

Remarks

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.

Examples

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;
});

.NET Framework Security

See Also

Reference

JoinableTaskContext Class

Microsoft.VisualStudio.Threading Namespace