Class ThreadingHelper

Provides methods for running code on other threads and synchronizing with the main thread.

Inheritance
System.Object ThreadingHelper
Implements
System.ComponentModel.ISynchronizeInvoke
Namespace: BepInEx
Assembly: BepInEx.dll
Syntax
public sealed class ThreadingHelper : MonoBehaviour, ISynchronizeInvoke

Properties

Instance

Current instance of the helper.

Declaration
public static ThreadingHelper Instance { get; }
Property Value
Type Description
ThreadingHelper

InvokeRequired

False if current code is executing on the main unity thread, otherwise True. Warning: Will return true before the first frame finishes (i.e. inside plugin Awake and Start methods).

Declaration
public bool InvokeRequired { get; }
Property Value
Type Description
System.Boolean

SynchronizingObject

Gives methods for invoking delegates on the main unity thread, both synchronously and asynchronously. Can be used in many built-in framework types, for example System.IO.FileSystemWatcher.SynchronizingObject and System.Timers.Timer.SynchronizingObject to make their events fire on the main unity thread.

Declaration
public static ISynchronizeInvoke SynchronizingObject { get; }
Property Value
Type Description
System.ComponentModel.ISynchronizeInvoke

Methods

StartAsyncInvoke(Func<Action>)

Queue the delegate to be invoked on a background thread. Use this to run slow tasks without affecting the game. NOTE: Most of Unity API can not be accessed while running on another thread!

Declaration
public void StartAsyncInvoke(Func<Action> action)
Parameters
Type Name Description
System.Func<System.Action> action

Task to be executed on another thread. Can optionally return an Action that will be executed on the main thread. You can use this action to return results of your work safely. Return null if this is not needed.

StartSyncInvoke(Action)

Queue the delegate to be invoked on the main unity thread. Use to synchronize your threads.

Declaration
public void StartSyncInvoke(Action action)
Parameters
Type Name Description
System.Action action

Explicit Interface Implementations

ISynchronizeInvoke.BeginInvoke(Delegate, Object[])

Declaration
IAsyncResult ISynchronizeInvoke.BeginInvoke(Delegate method, object[] args)
Parameters
Type Name Description
System.Delegate method
System.Object[] args
Returns
Type Description
System.IAsyncResult

ISynchronizeInvoke.EndInvoke(IAsyncResult)

Declaration
object ISynchronizeInvoke.EndInvoke(IAsyncResult result)
Parameters
Type Name Description
System.IAsyncResult result
Returns
Type Description
System.Object

ISynchronizeInvoke.Invoke(Delegate, Object[])

Declaration
object ISynchronizeInvoke.Invoke(Delegate method, object[] args)
Parameters
Type Name Description
System.Delegate method
System.Object[] args
Returns
Type Description
System.Object

Implements

System.ComponentModel.ISynchronizeInvoke