示例#1
0
        /// <summary>
        /// Checks if there is another instance of the Client running, either in GUI mode
        /// or in quiet mode.
        /// </summary>
        /// <param name="GUIMode">The mode in which to check if there are other instances running.</param>
        /// <returns>True if there is another instance of the Client running in the given mode.</returns>
        private static bool CheckForRunningInstances(bool GUIMode)
        {
            bool retVal = false;

            try
            {
                Process    CurrentProcess  = Process.GetCurrentProcess();
                Process [] ClientProcesses = Process.GetProcessesByName("CrawlWave.Client");
                if (ClientProcesses.Length == 1)
                {
                    //there is no other instance of the Client running :)
                    retVal = false;
                }
                if (GUIMode)
                {
                    //try to see if the other instances are running in GUI mode
                    foreach (Process ClientProcess in ClientProcesses)
                    {
                        try
                        {
                            if (ClientProcess.Id == CurrentProcess.Id)
                            {
                                //it's not meaningful to check the same instance!
                                continue;
                            }
                            if (ClientProcess.MainWindowTitle != "")
                            {
                                //if there is another instance of the Client running in GUI mode this one must terminate
                                MessageBox.Show("Another instance of CrawlWave Client is already running!\n This instance will now terminate.", "CrawlWave Client", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                retVal = true;
                                break;
                            }
                        }
                        catch
                        {
                            //try checking the next process
                            continue;
                        }
                    }
                }
                else                 //Quiet mode (no GUI)
                {
                    //try to see if the other instances are running in quiet mode
                    foreach (Process ClientProcess in ClientProcesses)
                    {
                        try
                        {
                            if (ClientProcess.Id == CurrentProcess.Id)
                            {
                                //it's not meaningful to check the same instance!
                                continue;
                            }
                            if (ClientProcess.MainWindowTitle != "")
                            {
                                //if there is another instance of the Client running
                                //in GUI mode wait for it to terminate
                                ClientProcess.WaitForExit();
                            }
                            else
                            {
                                //if there is another instance of the client running
                                //in quiet mode this instance must terminate
                                System.Windows.Forms.MessageBox.Show("Another instance of CrawlWave Client is already running!\n This instance will now terminate.", "CrawlWave Client", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                retVal = true;
                            }
                        }
                        catch
                        {
                            //try checking the next process
                            continue;
                        }
                    }
                }
            }
            catch
            {
                //assume that it's not safe to run
                retVal = true;
            }
            return(retVal);
        }