Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I hope this works for you. I assume your gui stuff is on one thread. Just initialize this singleton and rely on it whenever you feel like calling Control.InvokeRequired property.</p> <p>Cheers,</p> <p>-Greg</p> <pre><code>public sealed class UiThread { #region Singleton // Singleton pattern implementation taken from Jon Skeet's C# and .NET article www.yoda.arachsys.com/csharp/singleton.html UiThread() { } public static UiThread Instance { get { return Nested.instance; } } class Nested { // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Nested() { } internal static readonly UiThread instance = new UiThread(); } #endregion int _uiThreadId = 0; public void SetUiThread(Thread thread) { if (_uiThreadId != 0) throw new ApplicationException("UI Thread has already been established!"); if (thread.ManagedThreadId == 0) throw new ArgumentException("Unexpected thread id value of 0!"); if (thread.IsBackground) throw new ArgumentException("Supplied thread should not be a background thread!"); if (thread.IsThreadPoolThread) throw new ArgumentException("Supplied thread should not be a thread pool thread!"); _uiThreadId = thread.ManagedThreadId; } /// &lt;summary&gt; /// It's possible for InvokeRequired to return false when running in background thread. /// This happens when unmanaged control handle has not yet been created. /// We second-guess Microsoft's implementation in this case, checking against foreground thread's Id. /// &lt;/summary&gt; /// &lt;param name="control"&gt;Control to check against.&lt;/param&gt; public bool InvokeRequired(Control control) { if (control.InvokeRequired) return true; IntPtr unmanagedHandle = control.Handle; bool newResult = control.InvokeRequired; if (unmanagedHandle == IntPtr.Zero) { // Trace.WriteLine() call here forces unmanagedHandle's initialization, // even with optimizing compiler. Trace.WriteLine(string.Format("Control handle could not be established! Control was {0}.", control.ToString())); } bool retVal = InvokeRequired(); // Break whenever the result of our check does not match theirs. Debug.Assert(retVal == newResult); // Return our InvokeRequired result over theirs // to keep with the tradition of updating controls from foreground only. return retVal; } /// &lt;summary&gt; /// Prefer to use overload with Control argument if at all possible. /// It's possible for InvokeRequired to return false when running in background thread. /// This happens when unmanaged control handle has not yet been created. /// We second-guess Microsoft's implementation in this case, checking against foreground thread's Id. /// &lt;/summary&gt; public bool InvokeRequired() { if (_uiThreadId == 0) throw new ApplicationException("UI Thread has not been established!"); return (Thread.CurrentThread.ManagedThreadId != _uiThreadId); } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload