VB.NET:空闲时间外部应用程序

时间:2017-04-27 19:22:11

标签: vb.net pc idle-timer

我看到很多关于计算程序中空闲时间的帖子,但我想要做的是计算单独Windows程序的空闲时间。长话短说,我们有非常昂贵的会计软件的12个许可证。我们有大约20名员工,但不是每个人都使用该软件。我们将不时地使用所有12个许可证 - 并且公司中有一个层次结构,关于谁应该能够根据需要访问软件。

我的任务是编写一个程序,让我们开始#34;用户。我写了一个简单的程序,它将在后台运行并检查软件是否已加载。另一个程序只是通过共享网络驱动器发送命令,以告诉程序在必要时关闭。

有效。我现在被要求做的是包括软件的空闲时间。有没有办法监视外部应用程序的击键或鼠标点击?我想我基本上可以报告上次使用应用程序的时间,并从当前时间中减去空闲时间。但是,我正在努力弄清楚如何将任何密钥发送到特定应用程序?从逻辑上讲,我认为这就像一个键盘记录器(没有记录),但只有当特定应用程序处于焦点时才会这样。

有没有人有我可以探索的想法?我愿意做研究,但只是想知道是否有人知道任何API或其他技巧。

3 个答案:

答案 0 :(得分:1)

您可以在合理的循环中合并GetForgroundWindowGetLastInputInfo(Win32函数) 如果目标应用程序(目标)正在运行,并且目标不是前台窗口,那么我们可以假设应用程序处于空闲状态。只记录它从前景转换到背景的时间。

如果目标是前景窗口,那么您可以使用GetLastInputInfo来确定应用程序上次接收鼠标或键盘输入的时间。

**不幸的是MSDN现在已经关闭,所以我无法链接到文档。

答案 1 :(得分:0)

所以我的设置存储了用户日志的位置。如果我发现用户打开了一个应用程序(Axys),那么我用他们的windows用户名(.usr文件)创建一个文件。在该文件中,我保存上次应用程序处于活动状态。

    Dim strFileName As String = My.Settings.UserLogs & "\" & strWinUser & ".usr"
    Dim strTime As String = String.Empty
    lastInputInf.cbSize = Marshal.SizeOf(lastInputInf)
    lastInputInf.dwTime = 0
    GetLastInputInfo(lastInputInf)

        Dim Caption As New System.Text.StringBuilder(256)
        GetWindowText(GetForegroundWindow, Caption, Caption.Capacity)

        If Caption.ToString.Contains("Axys")  Then
            If (CInt((Environment.TickCount - lastInputInf.dwTime) / 1000)) > 0 Then
                Dim timeDbl As Double = CDbl((Environment.TickCount - lastInputInf.dwTime) / 1000)
                strTime = DateTime.Now.AddSeconds(-1 * timeDbl).ToString
            Else
                strTime = DateTime.Now.ToString
            End If

            Dim objWriter As New System.IO.StreamWriter(strFileName)
            objWriter.Write(strTime)
            objWriter.Close()

        Else
        End If

答案 2 :(得分:-1)

不确定这是否有所帮助,但可能通过微软的UPTIME.exe获得了良好的信息。它是一个跟踪正常运行时间,靴子,蓝屏等的工具。

这里有一个vbscript可能有一些线索来获取VB.NET的这些信息

我不是这里的作者: https://community.spiceworks.com/scripts/show/57-uptime-vbs

' ========================================================
' Script:           UpTime.vbs
' Description:      Create spreadsheet of uptime stats for all servers
' Note:         Change the variables below as required.
'               This program requires a copy of Microsoft's UPTIME.EXE tool available from http://support.microsoft.com/kb/232243
' Author:           Alan Kobb
'               Herley-CTI
' Originally created:   8/26/09
'  Copyright 2009, Herley-CTI.  ALL RIGHTS RESERVED.
' ========================================================

Set objShell = CreateObject("WScript.Shell")
Set dServers = CreateObject("Scripting.Dictionary")
Dim objExcel, objWorkbook, nPos, strXLFile, objWorksheet, sUpTime

' **********************************************************************************************************
' Variables to set values for

' Add an array element with the name of each server you want to track.  Set the DIM to the number of servers.
Dim aServers(2)
aServers(0) = "Server1"
aServers(1) = "Server2"

' The path and name of the Excel file created.
strXLFile = "C:\UpTimes.xls"

' How many days to track for each server.
nDays = "365"

' Path and filename of Uptime.exe (Required.  Download from Microsoft at http://support.microsoft.com/kb/232243)
sUpTime = "C:\tools\uptime.exe"

' **********************************************************************************************************

'REGION Open Excel File
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
Set objWorkbook = objExcel.Workbooks.Add()
Set objWorksheet = objWorkbook.Worksheets(1)
'ENDREGION
objWorksheet.Cells(2,1) = "Server"
objWorksheet.cells(1,2) = "% Time"
objWorksheet.cells(2,2) = "Avail"
objWorksheet.Cells(1,3) = "#"
objWorksheet.Cells(2,3) = "Reboot"
objWorksheet.Cells(1,4) = "Current"
objWorksheet.Cells(2,4) = "Uptime"
objWorksheet.Cells(3,4) = "(days)"
objWorksheet.Cells(2,5) = "MTBR"
objWorksheet.Cells(3,5) = "(days)"
objWorksheet.Cells(1,6) = "#"
objWorksheet.Cells(2,6) = "Bluescreens"
objWorksheet.Cells(1,7) = "Abnormal"
objWorksheet.Cells(2,7) = "Shutdowns"
objWorksheet.Cells(2,8) = "Reboots"
objWorksheet.Cells(2,9) = "Shutdowns"
objWorksheet.Cells(2,10) = "Notes"
nRow = 5

For i = 0 To UBound(aServers)
    If Len(aServers(i)) > 1 Then
        strServer = "\\" & aServers(i)

        strCmd = sUpTime & " /p:" & nDays & " " & strserver
        WScript.Echo strCmd

        Set objExec = objShell.Exec(strCmd)

        Do While objExec.Status = 0
            WScript.Sleep 2000
        Loop

        nAvailablePercent = 0
        nTotalReboots = 0
        nCurrentDays = 0
        nMTBR = 0
        bTooLittleData = False
        bInconsistant = False
        nBlues = 0
        nAbnormals = 0
        nBoots = 0
        nShuts = 0

        Do While Not(objExec.StdOut.AtEndOfStream)
            strLine = objExec.StdOut.ReadLine
            'WScript.Echo strLine
            If InStr(strLine,"Availability") >= 1 Then nAvailablePercent = field(strLine,":",2)
            If InStr(strLine,"Total Reboots") >= 1 Then nTotalReboots = field(strLine,":",2)
            If InStr(strLine,"Current System") >= 1 Then nCurrentDays = field(field(field(strLine,":",2),",",1),"d",1)
            If InStr(strLine,"Mean") >= 1 Then nMTBR = field(field(strLine,":",2),"d",1)
            If InStr(strLine,"earliest") >= 1 Then bTooLittleData = True
            If InStr(strLine,"Bluescreen ") >= 1 Then nBlues = nBlues + 1
            If InStr(strLine,"Abnormal") >= 1 Then nAbnormals = nAbnormals + 1
            If InStr(strLine,"Boot") >= 1 Then nBoots = nBoots + 1
            If InStr(strLine,"  Shutdown") >= 1 Then nShuts = nShuts + 1
        Loop

        objWorksheet.Cells(nRow,1) = Mid(strServer,3)
        objWorksheet.cells(nRow,2) = nAvailablePercent
        objWorksheet.Cells(nRow,3) = nTotalReboots
        objWorksheet.Cells(nRow,4) = nCurrentDays
        objWorksheet.Cells(nRow,5) = nMTBR
        objWorksheet.Cells(nRow,6) = nBlues
        objWorksheet.Cells(nRow,7) = nAbnormals
        objWorksheet.Cells(nRow,8) = nTotalReboots
        objWorksheet.Cells(nRow,9) = nShuts
        If bTooLittleData Then objWorksheet.Cells(nRow,10) = "Insufficient Data"
        nRow = nRow + 1
    End If
Next

' Save
objExcel.Visible = True
'objExcel.ActiveWorkbook.SaveAs strXLFile

Function Field(Str,Delim,Pos)
Dim aString
aString = Split(Str,Delim)
Field = aString(Pos-1)
End Function

我也想知道查询Windows Management Instrumentation是否会有所帮助。

我希望这里有所帮助。祝你好运。