예제 #1
0
        /// <summary>
        /// Releases all resources.
        /// </summary>
        public void Dispose() {
            _endpoint.Dispose();

            if (_process != null) {
                if (!_process.HasExited) {
                    _process.Kill();
                }
                _process.Dispose();
                _process = null;
            }
        }
예제 #2
0
        /// <summary>
        /// Stops the service.
        /// </summary>
        public void Quit(RemoteServer server) {
            if (_process == null || _process.HasExited)
                return;

            server.ShutDown();

            if (_process.WaitForExit(5000)) {
                _process.Dispose();
                _process = null;
            }
        }
예제 #3
0
        /// <summary>
        /// Stops the service.
        /// </summary>
        public void Quit() {
            if (_process == null || _process.HasExited)
                return;
            try {
                var request = (HttpWebRequest)WebRequest.Create(this.Uri + @"/shutdown");
                request.KeepAlive = false;
                WebResponse resp = request.GetResponse();
                resp.Close();
            } catch (WebException) { }

            if (_process.WaitForExit(5000)) {
                _process.Dispose();
                _process = null;
            }
        }
예제 #4
0
        /// <summary>
        /// Deletes a directory recursively by using a shell command.
        /// </summary>
        /// <param name="directory">Directory path</param>
        public static void DeleteDirectoryByShell(string directory)
        {
            if (!Directory.Exists(directory))
            {
                return;
            }

            Hashtable env = new Hashtable();

            env["PATH"] = Environment.GetFolderPath(Environment.SpecialFolder.System);

            var cmd = "cmd.exe /C RD /S /Q \"" + directory + "\"";

            ProcessExt.Execute(cmd, null, env, true);
        }
예제 #5
0
        public void Dispose() {
            if (_endpoint != null) {
                _endpoint.Dispose();
                _endpoint = null;
            }

            if (_firefox_process != null) {
                if (!_firefox_process.HasExited) {
                    _firefox_process.Kill();
                    _firefox_process.WaitForExit();
                }
                _firefox_process.Dispose();
                _firefox_process = null;
            }

            if (_profile_dir != null && !this.Persistant && Directory.Exists(_profile_dir)) {
                IOExt.DeleteDirectoryByShell(_profile_dir);
                _profile_dir = null;
            }
        }
예제 #6
0
        public void StartApplication(string firefoxPath, IEnumerable arguments, string profilePath) {
            Hashtable env = ProcessExt.GetStdEnvironmentVariables();
            env["TEMP"] = _working_dir;
            env["TMP"] = _working_dir;
            env["XRE_PROFILE_PATH"] = profilePath;
            env["MOZ_NO_REMOTE"] = "1";
            env["MOZ_CRASHREPORTER_DISABLE"] = "1";
            env["NO_EM_RESTART"] = "1";

            //start the process
            _firefox_process = ProcessExt.Start(firefoxPath, arguments, null, env, false, true);

            //Waits for the port to be listening
            SysWaiter.Wait(200);
            if (!_endpoint.WaitForListening(15000, 100))
                throw new Errors.TimeoutError("Firefox failed to open the listening port {0} within 15s", _endpoint);
        }
예제 #7
0
        public void Start(string filename, bool hide = true) {
            Hashtable env = ProcessExt.GetStdEnvironmentVariables();
            env["TEMP"] = _temp_folder;
            env["TMP"] = _temp_folder;

            string servicePath = Path.Combine(_library_dir, filename);
            if (!File.Exists(servicePath))
                throw new Errors.FileNotFoundError(servicePath);

            //Start the process
            _process = ProcessExt.Start(servicePath, _arguments, null, env, true, true);

            //Waits for the port to be listening
            if (!_endpoint.WaitForListening(10000, 150))
                throw new Errors.TimeoutError("The driver failed to open the listening port {0} within 10s", _endpoint);

        }
예제 #8
0
        /// <summary>
        /// Starts a process
        /// </summary>
        /// <param name="filepath">File path</param>
        /// <param name="args">Arguments</param>
        /// <param name="dir">Working dir. Inherits the current directory if null</param>
        /// <param name="env">Environement variables. Inherits all of them if null</param>
        /// <param name="noWindow">Hides the window if true</param>
        /// <param name="createJob">Creates a Job if true</param>
        /// <returns></returns>
        public static ProcessExt Start(string filepath, IEnumerable args
                                       , string dir, Hashtable env, bool noWindow, bool createJob)
        {
            string        cmd     = ProcessExt.BuildCommandLine(filepath, args);
            StringBuilder envVars = BuildEnvironmentVars(env);

            var si = new Native.STARTUPINFO();
            var pi = new Native.PROCESS_INFORMATION();

            int createFlags = Native.CREATE_UNICODE_ENVIRONMENT;

            if (noWindow)
            {
                createFlags |= Native.CREATE_NO_WINDOW;
            }

            IntPtr hJob    = IntPtr.Zero;
            bool   success = false;

            if (createJob)
            {
                IntPtr curProc = Native.GetCurrentProcess();

                bool isProcessInJob = false;
                success = Native.IsProcessInJob(curProc, IntPtr.Zero, out isProcessInJob);

                Native.CloseHandle(curProc);
                if (success)
                {
                    int createFlagsJob = createFlags | Native.CREATE_SUSPENDED;
                    if (isProcessInJob)
                    {
                        createFlagsJob |= Native.CREATE_BREAKAWAY_FROM_JOB;
                    }

                    success = Native.CreateProcess(null
                                                   , cmd
                                                   , IntPtr.Zero
                                                   , IntPtr.Zero
                                                   , false
                                                   , createFlagsJob
                                                   , envVars
                                                   , dir, si, pi);

                    if (success)
                    {
                        success = AssignProcessToNewJob(pi.hProcess, null, out hJob);
                        if (success)
                        {
                            if (-1 == Native.ResumeThread(pi.hThread))
                            {
                                throw new Win32Exception();
                            }
                        }
                        else
                        {
                            Native.TerminateProcess(pi.hProcess, -1);
                            Native.CloseHandle(pi.hProcess);
                            Native.CloseHandle(pi.hThread);
                            Native.CloseHandle(hJob);
                            hJob = IntPtr.Zero;
                        }
                    }
                }
            }

            if (!success)
            {
                success = Native.CreateProcess(null
                                               , cmd
                                               , IntPtr.Zero
                                               , IntPtr.Zero
                                               , false
                                               , createFlags
                                               , envVars
                                               , dir, si, pi);

                if (!success)
                {
                    throw new Win32Exception();
                }
            }

            return(new ProcessExt(pi.dwProcessId, pi.hProcess, hJob));
        }