我试图制作一个vb.net应用程序,在空闲时间的x秒/分钟后关闭一些应用程序 [英] I am trying to make a vb.net application that close some applications after x number of seconds/minutes of idle time

查看:93
本文介绍了我试图制作一个vb.net应用程序,在空闲时间的x秒/分钟后关闭一些应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试制作一个vb.net windows应用程序,在空闲时间的x秒/分钟后关闭一些应用程序(没有用户) PC上的交互)。有没有人有任何示例代码来执行此操作?



谢谢。

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.

推荐答案

循环处理和处理For Each Statement中的条件。我已经检查过wmplayer(这是Windows Media Player的进程名称)是否正在运行,如果是的话;我杀了这个过程。



另请查看有关空闲时间的帖子。这是一个非常难以确定的事情,因为任何应用程序都可以闲置几秒钟并重新恢复。所以我让你考虑自己的逻辑。然而,这篇文章可能会对方法



此代码经过测试并正常运行,如果有帮助,请标记为解决方案。



.NET

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 ' 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.


这篇关于我试图制作一个vb.net应用程序,在空闲时间的x秒/分钟后关闭一些应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆