gpt4 book ai didi

.net - 如何计算用户在应用程序上花费的总时间?

转载 作者:行者123 更新时间:2023-12-03 13:48:21 27 4
gpt4 key购买 nike

我想创建一个能够计算用户(即我自己)在特定应用程序(例如 Firefox)上花费的总时间的应用程序。如果用户在 Firefox 上花费大量时间(例如 1 小时或更长时间),此应用程序应显示警告消息

原因 : 我是一名 VB.NET 开发人员。在我的工作时间内,我的主要工具是 Visual Studio,我想进行编码。但是我偶尔需要 Firefox 来访问互联网(尤其是 SO 和其他网站)来为我的编程问题找到解决方案。问题是我沉迷于 SO 和 SO 浪费了我几个小时的时间 直到我忘记了我想继续编码而不是浏览 SO 站点。

我的问题 : 如何计算用户在 Firefox 等开放应用程序上花费的总时间?

更新:

如果我在 Firefox 上停留的时间过长,我需要播放一首歌曲作为警告信息。我的意图是创建一个 winform 或 windows 服务来实现这一点

最佳答案

这个人,Sateesh Arveti,编码了你要找的东西:Active Application Watcher :

Up to now, I have seen so many applications that will show system usage in terms of memory, processor...But, A user don't want this all details. He May expect to know how much time, he is spending on each application like browser, Winamp by the end of day...This application will help out a user to know how much time , he is spending on each application every day. This application assumes that the window, which is active as the application on which the user is working. So, it will log that application details like window title, process name and time spent on it in an xml file. It will continue like that until the application is closed. Once the application is closed, it will show entire active application's details in a browser with proper format.



这是我的简单 vb.net 版本(我为 FireFox 添加了声音警报事件)。

enter image description here

创建一个 WinTracker 类:
Imports System
Imports System.ComponentModel

Public Class WinTracker
Implements INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Public Event SoundAlert(ByVal sender As Object, ByVal e As EventArgs)

Private _ID As Integer
Private _Text As String
Private _ElapsedTime As TimeSpan
Private _LastStart As DateTime
Private _RunningTime As TimeSpan

Public Sub New(ByVal id As Integer, ByVal text As String)
_ID = id
_Text = text
Call StartTracking()
End Sub

ReadOnly Property ID() As Integer
Get
Return _ID
End Get
End Property

Property Text() As String
Get
Return _Text
End Get
Set(ByVal value As String)
If value <> _Text Then
_Text = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Text"))
End If
End Set
End Property

Public Sub StartTracking()
_RunningTime = TimeSpan.Zero
_LastStart = DateTime.Now
End Sub

Public Sub StopTracking()
_ElapsedTime += _RunningTime
_RunningTime = TimeSpan.Zero
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("ToString"))
End Sub

Public Sub UpdateTime()
_RunningTime = (DateTime.Now - _LastStart)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("ToString"))

If _RunningTime.Seconds >= 60 Then
RaiseEvent SoundAlert(Me, New EventArgs)
End If
End Sub

Public Overrides Function ToString() As String
Return "(" & FormatTimeSpan(_ElapsedTime + _RunningTime) & ") " & _Text
End Function

Public Shared Operator =(ByVal thisItem As WinTracker, ByVal thatItem As WinTracker) As Boolean
Return (thisItem.ID = thatItem.ID)
End Operator

Public Shared Operator <>(ByVal thisItem As WinTracker, ByVal thatItem As WinTracker) As Boolean
Return Not (thisItem.ID = thatItem.ID)
End Operator

Private Function FormatTimeSpan(ByVal span As TimeSpan) As String
Return span.Hours.ToString("00") & " hrs " & span.Minutes.ToString("00") & " min " & span.Seconds.ToString("00") & " sec"
End Function

Public Shared Sub SwitchWindows(ByVal FromWindow As WinTracker, ByVal ToWindow As WinTracker)
FromWindow.StopTracking()
ToWindow.StartTracking()
End Sub

End Class

然后创建一个带有计时器和列表框的表单:
Imports System
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Runtime.InteropServices

Public Class Form1
Private Declare Auto Function GetForegroundWindow Lib "user32" () As IntPtr
Private Declare Auto Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Int32, ByRef lpdwProcessId As Int32) As UInt32

Private _Windows As New BindingList(Of WinTracker)
Private _ActiveWindow As WinTracker

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
With ListBox1
.ValueMember = "ID"
.DisplayMember = "ToString"
.DataSource = New BindingSource(_Windows, Nothing)
End With
Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Dim hWnd As Integer = GetForegroundWindow().ToInt32

If hWnd > 0 Then
Dim id As Integer = 1
Call GetWindowThreadProcessId(hWnd, id)
If id > 0 Then
Dim text As String = Process.GetProcessById(id).ProcessName

If text <> String.Empty Then
Dim spent As WinTracker = _Windows.FirstOrDefault(Function(x As WinTracker) x.ID = id)
If spent Is Nothing Then
spent = New WinTracker(id, text)
_Windows.Add(spent)

If text.ToLower = "firefox" Then
AddHandler spent.SoundAlert, AddressOf WinTracker_SoundAlert
End If

Else
spent.Text = text
End If

If _ActiveWindow Is Nothing Then
_ActiveWindow = spent
Else
If _ActiveWindow <> spent Then
WinTracker.SwitchWindows(_ActiveWindow, spent)
_ActiveWindow = spent
Else
_ActiveWindow.UpdateTime()
End If
End If

End If
End If
End If
End Sub

Private Sub WinTracker_SoundAlert(ByVal sender As Object, ByVal e As EventArgs)
My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
End Sub

End Class

根据需要重构。

关于.net - 如何计算用户在应用程序上花费的总时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6471695/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com