// Enables windows in preparation of stopping modal.  If parameter is true, we enable all windows,
            // if false, only windows forms windows (i.e., windows controlled by this MsoComponent).
            // See also IMsoComponent.OnEnterState.
            internal void EnableWindowsForModalLoop(bool onlyWinForms, ApplicationContext context) {
                Debug.WriteLineIf(CompModSwitches.MSOComponentManager.TraceInfo, "ComponentManager : Leaving modal state");
                if (threadWindows != null) {
                    threadWindows.Enable(true);
                    Debug.Assert(threadWindows != null, "OnEnterState recursed, but it's not supposed to be reentrant");
                    threadWindows = threadWindows.previousThreadWindows;
                }

                ModalApplicationContext modalContext = context as ModalApplicationContext;
                if (modalContext != null) {
                    modalContext.DisableThreadWindows(false, onlyWinForms);
                }
            }
            // Disables windows in preparation of going modal.  If parameter is true, we disable all
            // windows, if false, only windows forms windows (i.e., windows controlled by this MsoComponent).
            // See also IMsoComponent.OnEnterState.
            internal void DisableWindowsForModalLoop(bool onlyWinForms, ApplicationContext context) {
                Debug.WriteLineIf(CompModSwitches.MSOComponentManager.TraceInfo, "ComponentManager : Entering modal state");
                ThreadWindows old = threadWindows;
                threadWindows = new ThreadWindows(onlyWinForms);
                threadWindows.Enable(false);
                threadWindows.previousThreadWindows = old;

                ModalApplicationContext modalContext = context as ModalApplicationContext;
                if (modalContext != null) {
                    modalContext.DisableThreadWindows(true, onlyWinForms);
                }
            }
            /// <include file='doc\Application.uex' path='docs/doc[@for="Application.ThreadContext.DisposeThreadWindows"]/*' />
            /// <devdoc>
            ///     Gets rid of all windows in this thread context.  Nulls out
            ///     window objects that we hang on to.
            /// </devdoc>
            internal void DisposeThreadWindows() {

                // We dispose the main window first, so it can perform any
                // cleanup that it may need to do.
                //
                try {
                    if (applicationContext != null) {
                        applicationContext.Dispose();
                        applicationContext = null;
                    }

                    // Then, we rudely destroy all of the windows on the thread
                    //
                    ThreadWindows tw = new ThreadWindows(true);
                    tw.Dispose();

                    // And dispose the parking form, if it isn't already
                    //
                    DisposeParkingWindow();
                }
                catch {
                }
            }
		public static DialogResult ShowDialog(Form form, bool disposeForm)
		{
			DialogResult result = DialogResult.OK;

#if !NDOC && !DESIGN
			if (form == null)
			{
				throw new ArgumentNullException("form");
			}

			IntPtr lastWnd = Win32Window.GetActiveWindow();

			// it seems that Form.Close() method processes Window Queue itself that is why it is 
			// only way to be noticed the a dialog form was closed
			// If we're not supposed to dispose the form, we need to listen
			// for the closing event instead, as the form will already be disposed
			// by the time Closed is raised.
			if (disposeForm)
			{
				form.Closed += new EventHandler(ModalForm_Closed);
			}
			else
			{
				form.Closing += new System.ComponentModel.CancelEventHandler(ModalForm_Closing);					
			}

			form.Show();
			form.Capture = true;
			IntPtr hwnd = Win32Window.GetCapture();
			form.Capture = false;

			ThreadWindows previousThreadWindows = threadWindows;
			threadWindows = new ThreadWindows(hwnd);
			threadWindows.previousThreadWindows = previousThreadWindows;
			threadWindows.Enable(false);

			// enters dialog window loop
			LocalModalMessageLoop();
			
			result = form.DialogResult;
           
			if(threadWindows != null)
			{
				threadWindows = threadWindows.previousThreadWindows;
			}

			if(disposeForm)
			{
				form.Closed -= new EventHandler(ModalForm_Closed);	
				form.Dispose();
			}
			else
			{
				form.Closing -= new System.ComponentModel.CancelEventHandler(ModalForm_Closing);
			}

			if (IsWindow(lastWnd) && IsWindowVisible(lastWnd))
			{
				SetActiveWindow(lastWnd);
			}
#endif
			return result;
		}