コード例 #1
0
        /// <summary>
        ///  Start and stop a process watcher.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        ///
        private void startStopItem_Click(object sender, EventArgs e)
        {
            if (processWatcher == null)
            {
                string processName = "Skype";
                processWatcher             = new ProcessWatcher(processName, 0.5);
                processWatcher.Started    += Process_Started;
                processWatcher.Terminated += Process_Terminated;

                titleTrackers = new Dictionary <int, TitleTracker>();

                Process[] processes = Process.GetProcessesByName(processName);
                log.Info("Running \"" + processName + "\" process instances: " + processes.Length);

                foreach (var process in processes)
                {
                    log.Info("Process id: " + process.Id);
                    TitleTracker titleTracker = new TitleTracker((uint)process.Id);
                    titleTracker.TitleChanged += TitleTracker_TitleChanged;
                    titleTrackers.Add(process.Id, new TitleTracker((uint)process.Id));
                }

                processWatcher.Start();
                startStopItem.Text = "Stop process watcher";
            }
            else
            {
                processWatcher.Dispose();
                processWatcher     = null;
                startStopItem.Text = "Start process watcher";
            }
        }
コード例 #2
0
 private void CleanupProcWatcher()
 {
     if (_procWatcher != null)
     {
         _procWatcher.ProcessCreated -= ProcWatcher_ProcessCreated;
         _procWatcher.ProcessDeleted -= ProcWatcher_ProcessDeleted;
         _procWatcher.Dispose();
         _procWatcher = null;
     }
 }
コード例 #3
0
            private void _Attach(Process p)
            {
                if (this.Process != null)
                {
                    try
                    {
                        int pid = this.Process.Id;
                        lock (activeProcesses)
                        {
                            activeProcesses.Remove(pid);
                        }
                    }
                    catch (Exception e)
                    {
                        Util.Logging.Log(e);
                    }

                    if (watcher != null)
                    {
                        watcher.Dispose();
                        watcher = null;
                    }
                    else
                    {
                        this.Process.Exited -= p_Exited;
                    }
                }

                bool hasExited  = false;
                bool hasStarted = false;

                try
                {
                    hasStarted = p.Id > 0;
                    hasExited  = p.HasExited;
                }
                catch (Exception ex)
                {
                    Util.Logging.Log(ex);
                }

                this.Process = p;

                if (!hasExited)
                {
                    bool supportsEvents = false;

                    if (!p.EnableRaisingEvents)
                    {
                        //try setting the value as the current user, then the user running the process
                        //the current user will be able to set another user's process if this process is still the owner (ownership is lost when this process is closed)
                        try
                        {
                            p.EnableRaisingEvents = true;
                            supportsEvents        = true;
                        }
                        catch (Exception e)
                        {
                            Util.Logging.Log(e);

                            if (!Util.Users.IsCurrentUser(account.Settings.WindowsAccount))
                            {
                                string username = Util.Users.GetUserName(account.Settings.WindowsAccount);

                                try
                                {
                                    using (Security.Impersonation.Impersonate(username, Security.Credentials.GetPassword(username)))
                                    {
                                        p.EnableRaisingEvents = true;
                                        supportsEvents        = true;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Util.Logging.Log(ex);
                                }
                            }
                        }
                    }
                    else
                    {
                        supportsEvents = true;
                    }

                    if (supportsEvents)
                    {
                        p.Exited += p_Exited;
                    }
                    else
                    {
                        watcher         = new ProcessWatcher(p);
                        watcher.Exited += p_Exited;
                    }

                    if (hasStarted && p.HasExited)
                    {
                        this.HasMutex = false;
                    }
                    else
                    {
                        this.HasMutex = true;
                    }
                }
                else
                {
                    this.HasMutex = false;

                    p_Exited(p, null);
                }

                if (hasStarted)
                {
                    lock (activeProcesses)
                    {
                        if (!p.HasExited)
                        {
                            activeProcesses.Add(p.Id, this);

                            if (ProcessActive != null)
                            {
                                ProcessActive(p, this.account);
                            }
                        }
                    }
                }
            }