0

Like monitoring the specific process and if the user was quit the process detect that it's not running any more and display it to the user.

In backgroundWorker1_DoWork I did:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
     BackgroundWorker worker = sender as BackgroundWorker;
      while (true)
      {
          if ((worker.CancellationPending == true))
          {
              e.Cancel = true;
              break;
          }
          else
          {
              Stopwatch sw = Stopwatch.StartNew();
              PopulateApplications();
              sw.Stop();
              int msec = 1000 - (int)sw.ElapsedMilliseconds;
              if (msec < 1) msec = 1;   // Don't consume 100% CPU
              System.Threading.Thread.Sleep(msec);
          }
      }
}

Then the PopulateApplication method:

private void PopulateApplications()
{
    this.BeginInvoke(new MethodInvoker(delegate
    {
         var icon = Icon.ExtractAssociatedIcon(GetProcessInfo(GetProcessIntptr()).ProcessFileName);
         Image ima = icon.ToBitmap();
         ima = resizeImage(ima, new Size(25, 25));
         ima = (Image)(new Bitmap(ima, new Size(25, 25)));
         String status = GetProcessInfo(GetProcessIntptr()).ProcessResponding ? "Running" : "Not Responding";
     }));
}

And the GetProcessInfo:

public ProcessInfo GetProcessInfo(IntPtr hwnd)
{
     uint pid = 0;
     GetWindowThreadProcessId(hwnd, out pid);
     Process proc = Process.GetProcessById((int)pid);

     return new ProcessInfo
     {               
         ProcessFileName = proc.MainModule.FileName.ToString(),
         ProcessName = proc.ProcessName,
         ProcessTitle = proc.MainWindowTitle,
         ProcessResponding = proc.Responding
     };
}

public class ProcessInfo
{
     public string ProcessName { get; set; }
     public string ProcessFileName { get; set; }
     public string ProcessTitle { get; set; }
     public bool ProcessResponding { get; set; }
}

I have a listBox2 control and I want to display there that if the current process is running show Running if the user was quit the process(application) then show instead Running something like Not running

The problem is that if the user quit the process then in this part:

return new ProcessInfo
{               
     ProcessFileName = proc.MainModule.FileName.ToString(),
     ProcessName = proc.ProcessName,
     ProcessTitle = proc.MainWindowTitle,
     ProcessResponding = proc.Responding
};

Since the process was quit but this is keep being calling from the backgroundworker dowork populateapplication method it will not get anymore the process info since the process no exist. And it will throw exception that:

the process not found

or in some other cases the program just will stop working without any exception.

I played with the PopulateApplications method the problem is that when i exit the process it take some seconds before the HasExited turn true.

bool isexited = false;
        Process[] pname;
        private void PopulateApplications()
        {
            this.BeginInvoke(new MethodInvoker(delegate
            {
                if (isexited == false)
                    pname = Process.GetProcessesByName(GetProcessInfo(GetProcessIntptr()).ProcessName);

                if (pname[0].HasExited)
                {
                    if (pname[0].Responding)
                    {
                        isexited = true;
                        pname[0].CloseMainWindow();
                        listBox2.Items.Add("Not Running");

                    }
                    else
                    {
                        isexited = true;
                        pname[0].Kill();
                        listBox2.Items.Add("Not Running");

                    }
                }
                else
                {
                    pname = Process.GetProcessesByName(GetProcessInfo(GetProcessIntptr()).ProcessName);
                    listBox2.Items.Add("Running");
                    var icon = Icon.ExtractAssociatedIcon(GetProcessInfo(GetProcessIntptr()).ProcessFileName);
                    Image ima = icon.ToBitmap();
                    ima = resizeImage(ima, new Size(25, 25));
                    ima = (Image)(new Bitmap(ima, new Size(25, 25)));
                }
            }));
        }
Manuel Spechia
  • 389
  • 1
  • 3
  • 16
  • Maybe I need to store the ProcessInfo Name,FileName,Title in a text file for example and then to keep check this vars if one of them or all of them empty or return some value that indicate that the process is not exist any more not running then to change the status var to "Not Running" ? – Manuel Spechia Jul 11 '15 at 13:32
  • 1
    What you show is a "polling" approach. It's inefficient. You should consider an asynchronous approach, where a background thread simply "waits" for the process to exit (by calling a method that blocks) and then signals the GUI to update. – Jonathon Reinhart Jul 11 '15 at 14:29
  • Is this what you want? http://stackoverflow.com/a/1986856/1159775 – hebinda Jul 11 '15 at 15:27
  • 1
    You will need to keep the Process object around so you can reliably detect that it terminated instead of re-creating it over and over again. – Hans Passant Jul 11 '15 at 16:56

0 Answers0