VB   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了.net – 如何计算用户在应用程序上花费的总时间?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建一个能够计算用户(即我自己)在特定应用程序上花费的总时间的应用程序,例如Firefox。如果用户在Firefox上花费大量时间(例如1小时以上),此应用程序应显示警告消息

原因:我是VB.NET开发人员。在工作时间,我的主要工具是Visual studio,我想做编码。但是我需要Firefox随时访问互联网(特别是SO和其他网站),以找到我的编程问题的解决方案。问题是我沉迷于SO和SO吸引了我几个小时的时间,直到我忘记了我想继续编码,而不是浏览SO站点。

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

更新:

如果我在Firefox上停留时间太长,我需要为自己播放一首警告信息。我的目的是创建一个winform或windows服务来实现这一点

这个家伙,Sateesh Arveti,编码你在找什么: Active Application Watcher

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

创建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 datetiR_980_11845@e
  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 = datetiR_980_11845@e.Now
  End Sub

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

  Public Sub UpdatetiR_980_11845@e()
    _RunningTime = (datetiR_980_11845@e.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.UpdatetiR_980_11845@e()
            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 – 如何计算用户在应用程序上花费的总时间?全部内容,希望文章能够帮你解决.net – 如何计算用户在应用程序上花费的总时间?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。