Example #1
0
        /// <summary>
        /// Closes process by process name.
        /// </summary>
        /// <returns></returns>
        public static void CloseProcess(Process processToClose)
        {
            try
            {
                if (processToClose != null)         // && !processToClose.HasExited)
                {
                    Logger.AddInformation("Closing " + processToClose.ProcessName);
                    if (processToClose.ProcessName.Contains("xulrunner"))
                    {
                        Logger.AddInformation("Closing XulRunner over Socket");
                        SEBXULRunnerWebSocketServer.SendAllowCloseToXulRunner();
                        Thread.Sleep(500);
                    }

                    string name = "processHasExitedTrue";

                    // Try to close process nicely with CloseMainWindow
                    if (!processToClose.HasExited)
                    {
                        Logger.AddInformation("Process " + processToClose.ProcessName + " hasnt closed yet, try again");
                        //If the process handles the mainWindow
                        if (processToClose.MainWindowHandle != IntPtr.Zero)
                        {
                            name = processToClose.ProcessName;
                            // Close process by sending a close message to its main window.
                            Logger.AddError("Send CloseMainWindow to process " + name, null, null);
                            processToClose.CloseMainWindow();
                            // Wait max. 5 seconds till the process exits
                            for (int i = 0; i < 5; i++)
                            {
                                processToClose.Refresh();
                                // If process still hasn't exited, we wait another second
                                if (processToClose != null && !processToClose.HasExited)
                                {
                                    Logger.AddError("Process " + name + " hasn't exited by closing its main window, wait up to one more second and check again.", null, null);
                                    //Thread.Sleep(1000);
                                    processToClose.WaitForExit(1000);
                                }
                                else
                                {
                                    Logger.AddError("Process " + name + " has exited.", null, null);
                                    break;
                                }
                            }
                        }
                    }
                    processToClose.Refresh();

                    // Check if process has exited now and otherwise kill it
                    if (!processToClose.HasExited)
                    {
                        // If process still hasn't exited, we kill it
                        Logger.AddError("Send Kill to process " + name, null, null);
                        processToClose.Kill();
                        // Wait max. 10 seconds till the process exits
                        for (int i = 0; i < 10; i++)
                        {
                            processToClose.Refresh();
                            // If process still hasn't exited, we wait another second
                            if (!processToClose.HasExited)
                            {
                                Logger.AddError("Process " + name + " still hasn't exited, wait up to one more second and check again.", null, null);
                                //Thread.Sleep(1000);
                                try
                                {
                                    processToClose.WaitForExit(1000);
                                }
                                catch (Exception ex)
                                {
                                    Logger.AddError("Unable to processToClose.WaitForExit(1000)", null, ex);
                                }
                            }
                            else
                            {
                                Logger.AddError("Process " + name + " has exited.", null, null);
                                break;
                            }
                        }
                    }
                    processToClose.Refresh();

                    // If process still hasn't exited or wasn't closed, we log this
                    if (!processToClose.HasExited)
                    {
                        //Logger.AddError("Process " + name + " has not exited after trying to close its main window and killing it and waiting in total 20 seconds!", null, null);
                        Logger.AddError("Process " + name + " has not exited after killing it and waiting in total 11 seconds!", null, null);
                    }

                    //// Close the process if it has exited (freeing resources)
                    //if (processToClose.HasExited)
                    //    //if (processToClose.HasExited && processToClose != SEBClientInfo.SebWindowsClientForm.xulRunner)
                    //    {
                    //    Logger.AddError("Send Close to process " + name, null, null);
                    //    processToClose.Close();

                    //    // Wait max. 10 seconds till the process exits
                    //    for (int i = 0; i < 10; i++)
                    //    {
                    //        processToClose.Refresh();
                    //        // If process still wasn't closed, we wait another second
                    //        if (processToClose.Handle != IntPtr.Zero)
                    //        {
                    //            Logger.AddError("Process " + name + " still wasn't closed, wait up to one more second and check again.", null, null);
                    //            //Thread.Sleep(1000);
                    //            processToClose.WaitForExit(1000);
                    //        }
                    //        else
                    //        {
                    //            Logger.AddError("Process " + name + " was successfully closed.", null, null);
                    //            break;
                    //        }
                    //    }

                    //    // If process still wasn't closed, we log this
                    //    processToClose.Refresh();
                    //    if (processToClose.Handle != IntPtr.Zero)
                    //    {
                    //        Logger.AddError("Process " + name + " didn't close.", null, null);
                    //    }
                    //}
                    //else
                    //{
                    //    Logger.AddError("Although it didn't exit yet, send Close anyways to process " + name, null, null);
                    //    processToClose.Close();
                    //}
                }
            }
            catch (Exception ex)
            {
                Logger.AddError("Error when killing process", null, ex);
            }
        }
Example #2
0
 public static void CloseProcess(Process processToClose)
 {
     try
     {
         if (processToClose == null)
         {
             return;
         }
         Logger.AddInformation("Closing " + processToClose.ProcessName, (object)null, (Exception)null, (string)null);
         if (processToClose.ProcessName.Contains("xulrunner"))
         {
             Logger.AddInformation("Closing XulRunner over Socket", (object)null, (Exception)null, (string)null);
             SEBXULRunnerWebSocketServer.SendAllowCloseToXulRunner();
             Thread.Sleep(500);
         }
         string str = "processHasExitedTrue";
         if (!processToClose.HasExited)
         {
             Logger.AddInformation("Process " + processToClose.ProcessName + " hasnt closed yet, try again", (object)null, (Exception)null, (string)null);
             if (processToClose.MainWindowHandle != IntPtr.Zero)
             {
                 str = processToClose.ProcessName;
                 Logger.AddError("Send CloseMainWindow to process " + str, (object)null, (Exception)null, (string)null);
                 processToClose.CloseMainWindow();
                 for (int index = 0; index < 5; ++index)
                 {
                     processToClose.Refresh();
                     if (processToClose != null && !processToClose.HasExited)
                     {
                         Logger.AddError("Process " + str + " hasn't exited by closing its main window, wait up to one more second and check again.", (object)null, (Exception)null, (string)null);
                         processToClose.WaitForExit(1000);
                     }
                     else
                     {
                         Logger.AddError("Process " + str + " has exited.", (object)null, (Exception)null, (string)null);
                         break;
                     }
                 }
             }
         }
         processToClose.Refresh();
         if (!processToClose.HasExited)
         {
             Logger.AddError("Send Kill to process " + str, (object)null, (Exception)null, (string)null);
             processToClose.Kill();
             for (int index = 0; index < 10; ++index)
             {
                 processToClose.Refresh();
                 if (!processToClose.HasExited)
                 {
                     Logger.AddError("Process " + str + " still hasn't exited, wait up to one more second and check again.", (object)null, (Exception)null, (string)null);
                     try
                     {
                         processToClose.WaitForExit(1000);
                     }
                     catch (Exception ex)
                     {
                         Logger.AddError("Unable to processToClose.WaitForExit(1000)", (object)null, ex, (string)null);
                     }
                 }
                 else
                 {
                     Logger.AddError("Process " + str + " has exited.", (object)null, (Exception)null, (string)null);
                     break;
                 }
             }
         }
         processToClose.Refresh();
         if (processToClose.HasExited)
         {
             return;
         }
         Logger.AddError("Process " + str + " has not exited after killing it and waiting in total 11 seconds!", (object)null, (Exception)null, (string)null);
     }
     catch (Exception ex)
     {
         Logger.AddError("Error when killing process", (object)null, ex, (string)null);
     }
 }
Example #3
0
        /// <summary>
        /// Closes process by process name.
        /// </summary>
        /// <returns></returns>
        public static bool CloseProcess(Process processToClose)
        {
            try
            {
                if (processToClose == null) // && !processToClose.HasExited)
                {
                    return(true);
                }
                else
                {
                    string name = "processHasExitedTrue";
                    name = processToClose.ProcessName;
                    Logger.AddInformation("Closing " + name);
                    if (name.Contains("xulrunner"))
                    {
                        Logger.AddInformation("Closing XULRunner over Socket");
                        SEBXULRunnerWebSocketServer.SendAllowCloseToXulRunner();
                        Thread.Sleep(500);
                    }

                    // Try to close process nicely with CloseMainWindow
                    try
                    {
                        if (processToClose.HasExited)
                        {
                            return(true);
                        }
                    }
                    catch (Exception)
                    {
                        Logger.AddInformation("Send Kill to process " + name);
                        // processToClose.Kill();
                        Logger.AddInformation("Successfully sent Kill to process " + name);
                        processToClose.Refresh();
                    }

                    Logger.AddInformation("Process " + name + " hasn't exited yet, try again");
                    //If the process handles the mainWindow
                    if (processToClose != null && !processToClose.HasExited && processToClose.MainWindowHandle != IntPtr.Zero)
                    {
                        // Close process by sending a close message to its main window.
                        Logger.AddError("Send CloseMainWindow to process " + name, null, null);
                        processToClose.CloseMainWindow();
                        // Wait max. 5 seconds till the process exits
                        for (int i = 0; i < 5; i++)
                        {
                            processToClose.Refresh();
                            // If process still hasn't exited, we wait another second
                            if (processToClose != null && !processToClose.HasExited)
                            {
                                Logger.AddError("Process " + name + " hasn't exited by closing its main window, wait up to one more second and check again.", null, null);
                                //Thread.Sleep(1000);
                                processToClose.WaitForExit(1000);
                            }
                            else
                            {
                                Logger.AddInformation("Process " + name + " has exited.", null, null);
                                break;
                            }
                        }
                    }

                    processToClose.Refresh();

                    // Check if process has exited now and otherwise kill it
                    if (!processToClose.HasExited)
                    {
                        // If process still hasn't exited, we kill it
                        Logger.AddInformation("Send Kill to process " + name);
                        // processToClose.Kill();
                        // Wait max. 10 seconds till the process exits
                        for (int i = 0; i < 10; i++)
                        {
                            processToClose.Refresh();
                            // If process still hasn't exited, we wait another second
                            if (!processToClose.HasExited)
                            {
                                Logger.AddError("Process " + name + " still hasn't exited, wait up to one more second and check again.", null, null);
                                //Thread.Sleep(1000);
                                try
                                {
                                    processToClose.WaitForExit(1000);
                                }
                                catch (Exception ex)
                                {
                                    Logger.AddError("Unable to processToClose.WaitForExit(1000)", null, ex);
                                }
                            }
                            else
                            {
                                Logger.AddError("Process " + name + " has exited.", null, null);
                                break;
                            }
                        }
                    }
                    processToClose.Refresh();

                    // If process still hasn't exited or wasn't closed, we log this
                    if (processToClose.HasExited)
                    {
                        Logger.AddInformation("Successfully closed process " + name);
                        return(true);
                    }
                    else
                    {
                        //Logger.AddError("Process " + name + " has not exited after trying to close its main window and killing it and waiting in total 20 seconds!", null, null);
                        Logger.AddError("Process " + name + " has not exited after killing it and waiting in total 11 seconds!", null, null);
                    }

                    return(false);
                }
            }
            catch (Exception ex)
            {
                Logger.AddError("Error when killing process", null, ex);
                return(false);
            }
        }