VB.NET:如何知道时间,而系统处于闲置状态? [英] VB.NET: How to know time for which system is idle?

查看:397
本文介绍了VB.NET:如何知道时间,而系统处于闲置状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序中,我实施自动显示器关闭在系统空闲时,即当用户不与系统进行交互。

I'm making an application in which I'm implementing auto-monitor turn off when system is idle, i.e. when user is not interacting with the system.

我找到了一个链接: HTTP://www.$c$cproject .COM / KB /系统/ SystemIdleTimerComponent.aspx

它确实提供了componenent知道,当系统处于闲置状态。但是,当我有:

it does provides the componenent to know when system is idle. But when I include:

Public WM_SYSCOMMAND As Integer = &H112
Public SC_MONITORPOWER As Integer = &Hf170

<DllImport("user32.dll")> _
Private Shared Function SendMessage(hWnd As Integer, hMsg As Integer, wParam As Integer, lParam As Integer) As Integer
End Function

Private Sub button1_Click(sender As Object, e As System.EventArgs)
    SendMessage(Me.Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, 2)
End Sub

这显示了此错误:跨线程操作无效:控制'Form1中'从一个线程比它创建于其他线程访问

推荐答案

我LastInputInfo通过使用一个定时器来检索它的每500毫秒在我的应用程序工作, 在code是这样的:

I got LastInputInfo to work in my application by using a timer to retrieve it every 500ms, the code looks like this :

Private ATimer As DispatcherTimer

Public Sub New()
        ....

    ATimer = New DispatcherTimer
    AddHandler ATimer.Tick, AddressOf Me.ATimer_Tick
    ATimer.Interval = TimeSpan.FromMilliseconds(500)  'Checks for idle every 500ms
    ATimer.Start()
End Sub


Public Structure LASTINPUTINFO
    Public cbSize As Integer
    Public dwTime As Integer
End Structure

Private Declare Function GetTickCount Lib "kernel32" () As Long

Public Declare Function GetLastInputInfo Lib "User32.dll" _
                              (ByRef lii As LASTINPUTINFO) As Boolean

Private Sub ATimer_Tick(ByVal sender As Object, ByVal e As EventArgs)

    MyLastInputInfo = New LASTINPUTINFO
    MyLastInputInfo.cbSize = Runtime.InteropServices.Marshal.SizeOf(MyLastInputInfo)

   ' get last input info from Windows
    If GetLastInputInfo(MyLastInputInfo) Then     ' if we have an input info     
       ' compute idle time
       Dim sysIdleTime_ms As Integer = (GetTickCount() - MyLastInputInfo.dwTime)

    End if
       ... Now you have the idle time in ms, do whatever you want with it :=)

什么可能是这种做事方式的局限在于,因为时间是在一个32位整数存储在蜱,这是唯一的'电脑(......)最后重新启动后工作了50天。 我的猜测是,虽然无论是计时单位计数和dwTime将包裹时,计时单位计数包装,因此没有问题,但无法测试。

What MAY be a limitation of this way of doing things is that since times are stored in ticks within a 32bits integer, it 'only' works for 50 days after the last reboot of your computer (...). My guess though is that both the TickCount and the dwTime will wrap when the TickCount wraps, hence no issue, but could not test.

这篇关于VB.NET:如何知道时间,而系统处于闲置状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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