On process count change event?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tyler Wiebe
    New Member
    • Mar 2011
    • 66

    On process count change event?

    I'm wondering if there is any event that my application can use to tell if a different process starts.

    More specifically, I want to know when another process, let's say Notepad, appears in the System.Diagnost ics.Process.Get Processes(), and possibly when it disappears.

    I'm mostly interested when the process appears, but when it disappears would be nice to.

    Any information about this would be much appreciated.
  • rekedtechie
    New Member
    • Feb 2012
    • 51

    #2
    im a newbie in c#
    and im not sure..
    but when i need to openread a file..
    i code it like this.. :)

    Code:
    using System;
    using System.IO;
    
    public class tst
    {
    public static void main(string[] args)
    {
    FileStream fl1 = null;
    FileStream fl2 = null;
    try {
    fl1 = file.OpenRead("myfile1.txt");
    Console.WriteLine("fl1 open");
    fl2 = file.OpenRead("myfile2.txt");
    Console.WriteLine("fl2 open");
    }
    catch(Exception er)
    {
    Console.Write("{0}",er.toString());
    }
    finally
    {
    if(fl1 != null) {
    fl1.Close();
    Console.WriteLine("fl1 closed");
    }
    if(fl2 != null) {
    fl2.Close();
    Console.WriteLine("fl2 closed");
    }
    }
    
    }
    }

    Comment

    • RhysW
      New Member
      • Mar 2012
      • 70

      #3
      rekedtechie, he isnt trying to open a file to read or write to, though his mention of notepad may have thrown you. If you open the task manager

      (ctrl+alt+ecs keys)
      and click the second tab along called Processes, the asker wants to be able to know when another process is added to this list, or removed.

      On topic, This youtube video shows you how to make a basic task manager as do the related videos, whilst this isnt what you are looking for it will contain the code you need to use, instead of giving the code i gave the link as it will probably help to solve future questions about similar, yet different, problems, where this video covers many more than i could.

      Comment

      • Tyler Wiebe
        New Member
        • Mar 2011
        • 66

        #4
        Well, thanks for the replies, and RhysW is correct, I am not trying to open a file. And yes, now that I think about it, saying Notepad may not have been the best example.

        Anyway, this morning I wrote a simple quick function that can do what I want, however it's not exactly what I was asking for, but it gets the job done with no problems.
        Code:
                public class Process
            {
                #region Constructors
        
                public Process(string Name)
                {
                    this.Name = Name;
                    this.Timer.Start();
                }
        
                #endregion
                #region Events
        
                public event Handlers.Started Started;
                public event Handlers.Ended Ended;
        
                #endregion
                #region Functions
        
                private void Process_Update()
                {
                    this.HasStarted = this.Exists;
                }
        
                public bool Terminate()
                {
                    try
                    {
                        foreach (System.Diagnostics.Process Process in System.Diagnostics.Process.GetProcessesByName(this.Name))
                        {
                            Process.Kill();
                        }
                        return true;
                    }
                    catch { return false; }
                }
        
                #endregion
                #region Handlers
        
                public class Handlers
                {
                    public delegate void Started();
                    public delegate void Ended();
                }
        
                #endregion
                #region Variables
        
                private System.Timers.Timer PTimer;
                private System.Timers.Timer Timer
                {
                    get
                    {
                        if (this.PTimer == null)
                        {
                            this.PTimer = new System.Timers.Timer();
                            this.PTimer.Interval = 1;
                            this.PTimer.Elapsed += delegate { this.Process_Update(); };
                        }
                        return this.PTimer;
                    }
                }
        
                public int Count
                {
                    get { return System.Diagnostics.Process.GetProcessesByName(this.Name).Length; }
                }
                public bool Exists
                {
                    get
                    {
                        if (this.Count > 0) return true;
                        else return false;
                    }
                }
                private bool PHasStarted;
                private bool HasStarted
                {
                    get { return this.PHasStarted; }
                    set
                    {
                        if (this.PHasStarted != value)
                        {
                            this.PHasStarted = value;
                            
                            if (this.PHasStarted && this.Started != null) this.Started();
                            if (!this.PHasStarted && this.Ended != null) this.Ended();
                        }
                    }
                }
        
                public string Name { get; private set; }
        
                #endregion
            }

        I'm not sure if this can be improved, but any improvements on this would be much appreciated.
        Last edited by Tyler Wiebe; Apr 24 '12, 07:00 PM. Reason: Wrong code snippet.

        Comment

        Working...