Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am trying to make a vb.net windows application that close some applications after x number of seconds/minutes of idle time (no user interaction) on the PC. Does anyone has any sample code to do this?

Thanks.
Posted
Comments
Shweta N Mishra 14-Nov-14 3:59am    
brief you situation. Are the application you are trying to close been open by you app ?

How they ran. what kind of application are those.

1 solution

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
VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim ProcessName As String = Nothing ' Name of process
    Dim ProcessFileName As String = Nothing ' Address of process
    ' Loop the processes list
    For Each FsProcess As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses
        Try ' Catch any errors, deal with processes you don't have rights to close.
            ProcessName = FsProcess.ProcessName.ToString
            ProcessFileName = FsProcess.MainModule.FileName.ToString
        Catch ex As Exception
            ' Exception is handled here.
        End Try
        Debug.WriteLine(FsProcess.ProcessName) ' For debug purpose only.
        If FsProcess.ProcessName = "wmplayer" Then
            FsProcess.Kill()
            Debug.WriteLine("Process: " & FsProcess.ProcessName & " Was Closed.") ' For debug purpose only.
        End If
    Next
End Sub


C#:
C#
private void Button1_Click(object sender, EventArgs e)
{
	string ProcessName = null;
	// Name of process
	string ProcessFileName = null;
	// Address of process
	// Loop the processes list
	foreach (System.Diagnostics.Process FsProcess in System.Diagnostics.Process.GetProcesses) {
		// Catch any errors, deal with processes you don't have rights to close. 
		try {
			ProcessName = FsProcess.ProcessName.ToString;
			ProcessFileName = FsProcess.MainModule.FileName.ToString;
		} catch (Exception ex) {
			// Exception is handled here.
		}
		Debug.WriteLine(FsProcess.ProcessName);
		// For debug purpose only.
		if (FsProcess.ProcessName == "wmplayer") {
			FsProcess.Kill();
			Debug.WriteLine("Process: " + FsProcess.ProcessName + " Was Closed.");
			// For debug purpose only.
		}
	}
}


If you have any questions, post back with a comment. Hope it helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900