JSSynchronizationContext class
Manages the synchronization context for a JavaScript environment, allowing callbacks and asynchronous continuations to be invoked on the JavaScript thread that runs the environment.
public abstract class JSSynchronizationContext : SynchronizationContext, IDisposable
Public Members
name | description |
---|---|
static Current { get; } | |
static Create() | |
IsDisposed { get; } | |
abstract CloseAsyncScope() | |
virtual Dispose() | |
abstract OpenAsyncScope() | |
Post(…) | Runs an action on the JS thread, without waiting for completion. (2 methods) |
Run(…) | Runs an action on the JS thread, and waits for completion. |
Run<T>(…) | Runs an action on the JS thread, and waits for the return value. |
RunAsync(…) | Runs an action on the JS thread, and asynchronously waits for completion. |
RunAsync<T>(…) | Runs an action on the JS thread, and asynchronously waits for the return value. |
Protected Members
name | description |
---|---|
JSSynchronizationContext() | The default constructor. |
Remarks
All JavaScript values are bound to the thread that runs the JS environment and can only be accessed from the same thread. Attempts to access a JavaScript value from a different thread will throw JSInvalidThreadAccessException
.
Use of Boolean) with continueOnCapturedContext:false
can prevent execution from returning to the JS thread, though it isn't necessarily a problem as long as there is a top-level continuation that uses continueOnCapturedContext:true
(the default) to return to the JS thread.
Code that makes explicit use of .NET threads or thread pools may need to capture the Current
context (before switching off the JS thread) and hold it for later use to call back to JS via Post
, Run
, or RunAsync
.