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)));
}
}));
}