C# Process Monitoring

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Prodian
    New Member
    • Jan 2008
    • 16

    C# Process Monitoring

    Im trying to monitor a process until the process ends but the program keeps freezing during the while loop and wont keep running. Heres what I have so far:

    Code:
    private void ProcessCheck()
            { 
                bool done = false;
                while (!done)
                    {
                       
                     System.Diagnostics.Process[] myProcesses;
                myProcesses =
                   System.Diagnostics.Process.GetProcessesByName("gsengine");   
                    
    if (myProcesses != null)
                        {
                            tabPage2.Enabled = false;
                            tabPage3.Enabled = false;
                            tabPage4.Enabled = false;
    
                            Start.Visible = false;
                            stop.Visible = true;
                            toolStripStatusLabel1.Text = "CORE RUNNING!";
                            
                        
                    }
    
                        if (myProcesses == null)
                        {
                            
                            tabPage2.Enabled = true;
                            tabPage3.Enabled = true;
                            tabPage4.Enabled = true;
    
                            Start.Visible = true;
                            stop.Visible = false;
    
                            toolStripStatusLabel1.Text = "CORE STOPPED FOR SOME REASON!";
    
                            done = true;
                        }
                }
            }
    Any ideas how I can get this working?
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    The code here is not looping, obviously. One option is to set done to true and in case null set to false

    Comment

    • Prodian
      New Member
      • Jan 2008
      • 16

      #3
      Ive used this method in other programs and it loops fine. I think the problem is the null part. If I put the done = true on both ifs it doesnt freeze but when its only on the == if, the program freezes.no errors. Is there another way Ican monitor to check to see if the process is still running?

      Comment

      • Prodian
        New Member
        • Jan 2008
        • 16

        #4
        If anybody is interested I got this working as well, at first it was using 100% cpu until I added the Thread.Sleep:

        Code:
        private void StillRunning(object result)
                {
        
        
                    if (!InvokeRequired)
                    {
                        
                            Start.Visible = false;
                        
                            stop.Visible = true;
                        
                            tabPage2.Enabled = false;
                        
                            tabPage3.Enabled = false;
                        
                            tabPage4.Enabled = false;
                            
                            toolStripStatusLabel1.Text = "GSMS STARTED!";
                            
                            wizardToolStripMenuItem.Enabled = false;
        
                        
                    }
                    else
                    {
                        Invoke(new StillRunningHandler(StillRunning), result);
        
                    }
        
                }
                private void First_Thread()
                {
                    Thread current_thread = Thread.CurrentThread;
        
                    bool done = false;
        
                    if (current_thread.IsAlive == true)  //Start of Thread is Alive
                    {
                        while (!done)               //Start of While Loop
                        {
                           Process[] pname = Process.GetProcessesByName("gsengine");
        
                            Thread.Sleep(500);
          
                            if (pname.Length == 0)          //Core Stopped, Do this.
                                {
                                   StoppedRunning(0);
                                    
                                    current_thread.Abort();     //Stop Thread NOW
        
                                    done = true;        //Get out of While Loop NOW
                                }
                            else                        //Core is running, Do this.
                            
                                {
          
                                StillRunning(0);
                                
                                }                    
                        }           //End of While Loop
                    }               //End of If Thread is Alive
                
                    else
                        {
                            toolStripStatusLabel1.Text = "Thread is not running, what happened?";
                        }
                }
        
                private void ThreadStart()
                {
                    ThreadStart thr_start_func = new ThreadStart(First_Thread);
                    Thread fThread = new Thread(thr_start_func);
                    fThread.Name = "first_thread";
                    fThread.Start();	//starting the thread
                    
                }

        Comment

        Working...