Loop the processes and handle the condition in the For Each Statement. I've checked to see if wmplayer (which is the process name for Windows Media Player) is running, and if it is; I kill the process.
Also look into this post regarding Idle times. This is a very difficult thing to determine, as any application can go idle for a number of seconds and resume again. So I've left you to consider your own logic for this. However, this post may offer some light on the
Approach.
This code is tested and working, please mark as a solution if it helped.
.NET
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ProcessName As String = Nothing
Dim ProcessFileName As String = Nothing
For Each FsProcess As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses
Try
ProcessName = FsProcess.ProcessName.ToString
ProcessFileName = FsProcess.MainModule.FileName.ToString
Catch ex As Exception
End Try
Debug.WriteLine(FsProcess.ProcessName)
If FsProcess.ProcessName = "wmplayer" Then
FsProcess.Kill()
Debug.WriteLine("Process: " & FsProcess.ProcessName & " Was Closed.")
End If
Next
End Sub
C#:
private void Button1_Click(object sender, EventArgs e)
{
string ProcessName = null;
string ProcessFileName = null;
foreach (System.Diagnostics.Process FsProcess in System.Diagnostics.Process.GetProcesses) {
try {
ProcessName = FsProcess.ProcessName.ToString;
ProcessFileName = FsProcess.MainModule.FileName.ToString;
} catch (Exception ex) {
}
Debug.WriteLine(FsProcess.ProcessName);
if (FsProcess.ProcessName == "wmplayer") {
FsProcess.Kill();
Debug.WriteLine("Process: " + FsProcess.ProcessName + " Was Closed.");
}
}
}
If you have any questions, post back with a comment. Hope it helps.