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

VB.NET: Idle Time external Application

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

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

有效。我现在被要求做的是包括软件的空闲时间。我有什么方法可以监控外部应用程序的击键或鼠标点击吗?我想我基本上可以报告上次使用应用程序的时间,并从当前时间中减去它来得出空闲时间。但是,我正在为如何确定是否有任何密钥发送到特定应用程序而苦苦挣扎?从逻辑上讲,我认为这就像一个键盘记录器(没有日志记录),但前提是特定应用程序是焦点。

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

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

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

** 不幸的是,MSDN 目前已关闭,因此我无法 link 访问文档。

不确定这是否有帮助,但也许 Microsoft 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 是否有帮助。

希望这里的内容能帮到您。祝你好运。

所以我将设置存储在用户日志的位置。如果我发现用户打开了一个应用程序 (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