Defines pinvoked utility methods and internal VS Constants
コード例 #1
0
        /// <summary>
        /// This is the thread method.
        /// </summary>
        private void HandleDialogBoxes()
        {
            // No synchronization numberOfDialogsToWaitFor since it is readonly
            var hwnds = new IntPtr[this.numberOfDialogsToWaitFor];
            var dialogBoxCloseResults = new bool[this.numberOfDialogsToWaitFor];

            try
            {
                // Signal that we started
                lock (Mutex)
                {
                    this.threadStarted.Set();
                }

                // The loop will be exited either if a message is send by the caller thread or if we found the dialog. If a message box text is specified the loop will not exit until the dialog is found.
                var stayInLoop           = true;
                var dialogBoxesToWaitFor = 1;

                while (stayInLoop)
                {
                    var hwndIndex = dialogBoxesToWaitFor - 1;

                    // We need to lock since the caller might set context to null.
                    lock (Mutex)
                    {
                        if (this.exitThread)
                        {
                            break;
                        }

                        // We protect the shell too from reentrency.
                        this.uiShell.GetDialogOwnerHwnd(out hwnds[hwndIndex]);
                    }

                    if (hwnds[hwndIndex] != IntPtr.Zero)
                    {
                        var windowClassName = new StringBuilder(256);
                        NativeMethods.GetClassName(hwnds[hwndIndex], windowClassName, windowClassName.Capacity);

                        // The #32770 is the class name of a messagebox dialog.
                        if (windowClassName.ToString().Contains("#32770"))
                        {
                            var unmanagedMemoryLocation = IntPtr.Zero;
                            var dialogBoxText           = String.Empty;
                            try
                            {
                                unmanagedMemoryLocation = Marshal.AllocHGlobal(10 * 1024);
                                NativeMethods.EnumChildWindows(hwnds[hwndIndex], new NativeMethods.CallBack(FindMessageBoxString), unmanagedMemoryLocation);
                                dialogBoxText = Marshal.PtrToStringUni(unmanagedMemoryLocation);
                            }
                            finally
                            {
                                if (unmanagedMemoryLocation != IntPtr.Zero)
                                {
                                    Marshal.FreeHGlobal(unmanagedMemoryLocation);
                                }
                            }

                            lock (Mutex)
                            {
                                // Since this is running on the main thread be sure that we close the dialog.
                                var dialogCloseResult = false;
                                if (this.buttonAction != 0)
                                {
                                    dialogCloseResult = NativeMethods.EndDialog(hwnds[hwndIndex], this.buttonAction);
                                }

                                // Check if we have found the right dialog box.
                                if (String.IsNullOrEmpty(this.expectedDialogBoxText) || (!String.IsNullOrEmpty(dialogBoxText) && String.Compare(this.expectedDialogBoxText, dialogBoxText.Trim(), StringComparison.OrdinalIgnoreCase) == 0))
                                {
                                    dialogBoxCloseResults[hwndIndex] = dialogCloseResult;
                                    if (dialogBoxesToWaitFor++ >= this.numberOfDialogsToWaitFor)
                                    {
                                        stayInLoop = false;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                //Let the main thread run a possible close command.
                System.Threading.Thread.Sleep(2000);

                foreach (var hwnd in hwnds)
                {
                    // At this point the dialog should be closed, if not attempt to close it.
                    if (hwnd != IntPtr.Zero)
                    {
                        NativeMethods.SendMessage(hwnd, NativeMethods.WM_CLOSE, 0, new IntPtr(0));
                    }
                }

                lock (Mutex)
                {
                    // Be optimistic.
                    this.dialogBoxCloseResult = true;

                    for (var i = 0; i < dialogBoxCloseResults.Length; i++)
                    {
                        if (!dialogBoxCloseResults[i])
                        {
                            this.dialogBoxCloseResult = false;
                            break;
                        }
                    }

                    this.threadDone.Set();
                }
            }
        }