Try SynchronizationContext.Current . It has Post and Send members, which roughly correspond to BeginInvoke and Invoke on Control . These operations will continue to function as long as the user interface thread is active against a specific control.
The SynchronizationContext type does not apply to WinForms, and its solutions will migrate to other environments such as WPF.
For instance.
BeginInvoke Code
void OnButtonClicked() { DoBackgroundOperation(this); } void DoBackgroundOperation(ISynchronizedInvoke invoke) { ThreadPool.QueueUserWorkItem(delegate { ... delegate.BeginInovke(new MethodInvoker(this.BackgroundOperationComplete), null); }); }
Sync Contact Code
void OnButtonClicked() { DoBackgroundOperation(SynchronizationContext.Current); } void DoBackgroundOperation(SynchronizationContext context) { ThreadPool.QueueUserWorkItem(delegate { ... context.Post(delegate { this.BackgroundOperationComplete() }, null); }); }
source share