internal static Application.ThreadContext FromId(int id)
 {
     Application.ThreadContext context = (Application.ThreadContext) contextHash[id];
     if ((context == null) && (id == System.Windows.Forms.SafeNativeMethods.GetCurrentThreadId()))
     {
         context = new Application.ThreadContext();
     }
     return context;
 }
 internal static Application.ThreadContext FromCurrent()
 {
     Application.ThreadContext currentThreadContext = Application.ThreadContext.currentThreadContext;
     if (currentThreadContext == null)
     {
         currentThreadContext = new Application.ThreadContext();
     }
     return currentThreadContext;
 }
 private static void ExitCommon(bool disposing)
 {
     lock (tcInternalSyncObject)
     {
         if (contextHash != null)
         {
             Application.ThreadContext[] array = new Application.ThreadContext[contextHash.Values.Count];
             contextHash.Values.CopyTo(array, 0);
             for (int i = 0; i < array.Length; i++)
             {
                 if (array[i].ApplicationContext != null)
                 {
                     array[i].ApplicationContext.ExitThread();
                 }
                 else
                 {
                     array[i].Dispose(disposing);
                 }
             }
         }
     }
 }
 internal void Dispose(bool postQuit)
 {
     lock (this)
     {
         try
         {
             if (this.disposeCount++ == 0)
             {
                 if ((this.messageLoopCount > 0) && postQuit)
                 {
                     this.PostQuit();
                 }
                 else
                 {
                     bool flag = System.Windows.Forms.SafeNativeMethods.GetCurrentThreadId() == this.id;
                     try
                     {
                         if (flag)
                         {
                             if (this.componentManager != null)
                             {
                                 this.RevokeComponent();
                             }
                             this.DisposeThreadWindows();
                             try
                             {
                                 Application.RaiseThreadExit();
                             }
                             finally
                             {
                                 if (this.GetState(1) && !this.GetState(2))
                                 {
                                     this.SetState(1, false);
                                     System.Windows.Forms.UnsafeNativeMethods.OleUninitialize();
                                 }
                             }
                         }
                     }
                     finally
                     {
                         if (this.handle != IntPtr.Zero)
                         {
                             System.Windows.Forms.UnsafeNativeMethods.CloseHandle(new HandleRef(this, this.handle));
                             this.handle = IntPtr.Zero;
                         }
                         try
                         {
                             if (totalMessageLoopCount == 0)
                             {
                                 Application.RaiseExit();
                             }
                         }
                         finally
                         {
                             contextHash.Remove(this.id);
                             if (currentThreadContext == this)
                             {
                                 currentThreadContext = null;
                             }
                         }
                     }
                 }
                 GC.SuppressFinalize(this);
             }
         }
         finally
         {
             this.disposeCount--;
         }
     }
 }
 public ThreadContext()
 {
     IntPtr zero = IntPtr.Zero;
     System.Windows.Forms.UnsafeNativeMethods.DuplicateHandle(new HandleRef(null, System.Windows.Forms.SafeNativeMethods.GetCurrentProcess()), new HandleRef(null, System.Windows.Forms.SafeNativeMethods.GetCurrentThread()), new HandleRef(null, System.Windows.Forms.SafeNativeMethods.GetCurrentProcess()), ref zero, 0, false, 2);
     this.handle = zero;
     this.id = System.Windows.Forms.SafeNativeMethods.GetCurrentThreadId();
     this.messageLoopCount = 0;
     currentThreadContext = this;
     contextHash[this.id] = this;
 }
 public void DisableThreadWindows(bool disable, bool onlyWinForms)
 {
     Control mainForm = null;
     if ((base.MainForm != null) && base.MainForm.IsHandleCreated)
     {
         IntPtr windowLong = System.Windows.Forms.UnsafeNativeMethods.GetWindowLong(new HandleRef(this, base.MainForm.Handle), -8);
         mainForm = Control.FromHandleInternal(windowLong);
         if ((mainForm != null) && mainForm.InvokeRequired)
         {
             this.parentWindowContext = Application.GetContextForHandle(new HandleRef(this, windowLong));
         }
         else
         {
             this.parentWindowContext = null;
         }
     }
     if (this.parentWindowContext != null)
     {
         if (mainForm == null)
         {
             mainForm = this.parentWindowContext.ApplicationContext.MainForm;
         }
         if (disable)
         {
             mainForm.Invoke(new ThreadWindowCallback(this.DisableThreadWindowsCallback), new object[] { this.parentWindowContext, onlyWinForms });
         }
         else
         {
             mainForm.Invoke(new ThreadWindowCallback(this.EnableThreadWindowsCallback), new object[] { this.parentWindowContext, onlyWinForms });
         }
     }
 }