vs-threading icon indicating copy to clipboard operation
vs-threading copied to clipboard

Add a NoThrowAwaitable for SwitchToMainThreadAsync

Open sharwell opened this issue 4 years ago • 3 comments

It will be easier to avoid first chance exceptions if a NoThrowAwaitable extension method can be added for MainThreadAwaitable. The Boolean result of this custom awaitable would be true if the continuation executes on the main thread, and false if the continuation happened on any other thread.

sharwell avatar Jul 01 '21 17:07 sharwell

💭 Should the method return a value indicating the current thread (true=main thread; false=other thread), or return a value indicating whether cancellation is requested (true=main thread and not cancelled; false=cancelled).

sharwell avatar Jul 06 '21 23:07 sharwell

I suppose it should be whether canceled or not, since folks seem mostly interested in whether to continue based on cancellation rather than whether they landed on the main thread before cancellation was processed (or were already there before they asked to switch). But I wonder in that case whether you need to return bool at all. The caller obviously already has a token they can check.

AArnott avatar Jul 07 '21 00:07 AArnott

If we keep the void result for the awaiter, I can merge this feature directly into the existing MainThreadAwaitable without making new awaitables (since GetResult() would keep the void return value).

sharwell avatar Jul 07 '21 19:07 sharwell