关于VB问题,希望高手帮解决
高分请高手帮解决一下这个问题,我是VB初学者,没接触过VB.NET
这里有一段VB.NET的代码要转成VB,用的API没接触过,代码的很多地方也不熟悉,虽然勉强可以看懂,但实在无从下手……所以请大家帮帮忙
很短的一个小程序
Public Class Form1
'API引用
Declare Auto Function CreateWaitableTimer Lib "kernel32.dll" _
(ByVal lpTimerAttributes As IntPtr, ByVal bManualReset As Boolean, ByVal lpTimerName As String) As SafeWaitHandle
Declare Auto Function SetWaitableTimer Lib "kernel32.dll" ( _
ByVal hTimer As SafeWaitHandle, _
<[In]()> ByRef pDueTime As Long, ByVal lPeriod As Integer, _
ByVal pfnCompletionRoutine As IntPtr, ByVal lpArgToCompletionRoutine As IntPtr, _
ByVal fResume As Boolean) As Boolean
Public Declare Auto Function WaitForSingleObject Lib "kernel32" (ByVal handle As SafeWaitHandle, ByVal milliseconds As UInteger) As Int32
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'新开线程进行休眠唤醒处理
Dim thread As New Thread(New ThreadStart(AddressOf SetWaitForWakeUpTime))
thread.Start() '定义唤醒时间
System.Threading.Thread.Sleep(2000) '等待2秒,保证线程中唤醒程序运行到等待状态
System.Windows.Forms.Application.SetSuspendState(PowerState.Suspend, True, False) '开始休眠
End Sub
''' <summary>
''' 唤醒处理
''' </summary>
''' <remarks></remarks>
Private Sub SetWaitForWakeUpTime()
'设置唤醒时间
Dim utc As DateTime = DateTime.Now.AddMinutes(1) '唤醒时间为2分钟之后
Dim duetime As Long = utc.ToFileTime()
Dim handle As SafeWaitHandle
handle = CreateWaitableTimer(IntPtr.Zero, True, "MyWaitabletimer")
If SetWaitableTimer(handle, duetime, 0, IntPtr.Zero, IntPtr.Zero, True) Then
Dim wh As EventWaitHandle = New EventWaitHandle(False, EventResetMode.AutoReset)
wh.SafeWaitHandle = handle
wh.WaitOne()
End If
'唤醒后的处理
DoWorkOnWakeupAfter()
End Sub
''' <summary>
''' 唤醒后的处理
''' </summary>
''' <remarks></remarks>
Private Sub DoWorkOnWakeupAfter()
'Do your Work
'启动一个测试程序。该程序窗体
Dim p As New Process()
p.StartInfo.FileName = "testStart.exe"
p.Start()
End Sub
End Class
追问:这位能不能帮回车一下..
答案:修改了一个网上找到的类,不完全是翻译,做到了等效的延迟效果,在窗体画一个按钮后加入以下代码↓
Private Type FILETIME dwLowDateTime As Long dwHighDateTime As LongEnd Type
Private Const WAIT_ABANDONED& = &H80&Private Const WAIT_ABANDONED_0& = &H80&Private Const WAIT_FAILED& = -1&Private Const WAIT_IO_COMPLETION& = &HC0&Private Const WAIT_OBJECT_0& = 0Private Const WAIT_OBJECT_1& = 1Private Const WAIT_TIMEOUT& = &H102&Private Const INFINITE = &HFFFFPrivate Const ERROR_ALREADY_EXISTS = 183&Private Const QS_HOTKEY& = &H80Private Const QS_KEY& = &H1Private Const QS_MOUSEBUTTON& = &H4Private Const QS_MOUSEMOVE& = &H2Private Const QS_PAINT& = &H20Private Const QS_POSTMESSAGE& = &H8Private Const QS_SENDMESSAGE& = &H40Private Const QS_TIMER& = &H10Private Const QS_MOUSE& = (QS_MOUSEMOVE Or QS_MOUSEBUTTON)Private Const QS_INPUT& = (QS_MOUSE Or QS_KEY)Private Const QS_ALLEVENTS& = (QS_INPUT Or QS_POSTMESSAGE Or QS_TIMER Or QS_PAINT Or QS_HOTKEY)Private Const QS_ALLINPUT& = (QS_SENDMESSAGE Or QS_PAINT Or QS_TIMER Or QS_POSTMESSAGE Or QS_MOUSEBUTTON Or QS_MOUSEMOVE Or QS_HOTKEY Or QS_KEY)
Private Const UNITS = 4294967296#Private Const MAX_LONG = -2147483648#
Private Declare Function CreateWaitableTimer Lib "kernel32" Alias "CreateWaitableTimerA" (ByVal lpSemaphoreAttributes As Long, ByVal bManualReset As Long, ByVal lpName As String) As LongPrivate Declare Function OpenWaitableTimer Lib "kernel32" Alias "OpenWaitableTimerA" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As String) As LongPrivate Declare Function SetWaitableTimer Lib "kernel32" (ByVal hTimer As Long, lpDueTime As FILETIME, ByVal lPeriod As Long, ByVal pfnCompletionRoutine As Long, ByVal lpArgToCompletionRoutine As Long, ByVal fResume As Long) As LongPrivate Declare Function CancelWaitableTimer Lib "kernel32" (ByVal hTimer As Long)Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As LongPrivate Declare Function MsgWaitForMultipleObjects Lib "user32" (ByVal nCount As Long, pHandles As Long, ByVal fWaitAll As Long, ByVal dwMilliseconds As Long, ByVal dwWakeMask As Long) As Long
Private mlTimer As Long
Public Sub Wait(MilliSeconds As Long) On Error Resume Next Dim ft As FILETIME Dim lBusy As Long Dim lRet As Long Dim dblDelay As Double Dim dblDelayLow As Double mlTimer = CreateWaitableTimer(0, True, App.EXEName & "Timer" & Format$(Now(), "NNSS")) If Err.LastDllError <> ERROR_ALREADY_EXISTS Then ft.dwLowDateTime = -1 ft.dwHighDateTime = -1 lRet = SetWaitableTimer(mlTimer, ft, 0, 0, 0, 0) End If '转换单位到纳秒 dblDelay = CDbl(MilliSeconds) * 10000#
ft.dwHighDateTime = -CLng(dblDelay / UNITS) - 1 dblDelayLow = -UNITS * (dblDelay / UNITS - Fix(CStr(dblDelay / UNITS))) If dblDelayLow < MAX_LONG Then dblDelayLow = UNITS + dblDelayLow ft.dwLowDateTime = CLng(dblDelayLow) lRet = SetWaitableTimer(mlTimer, ft, 0, 0, 0, False) Do lBusy = MsgWaitForMultipleObjects(1, mlTimer, False, INFINITE, QS_ALLINPUT&) DoEvents Loop Until lBusy = WAIT_OBJECT_0 '完成后关闭句柄 CloseHandle mlTimer mlTimer = 0 DoWorkOnWakeupAfterEnd Sub Private Sub DoWorkOnWakeupAfter() '唤醒后操作 MsgBox "我醒了"End Sub
Private Sub Command1_Click() Wait 120000 '唤醒时间为2分钟之后End Sub
Private Sub Form_Unload(Cancel As Integer) On Error Resume Next If mlTimer <> 0 Then CloseHandle mlTimer EndEnd Sub
找西北农林科技大学信息工程学院的陈勇老师,他肯定能解决!
上一个:这个VB程序具体代码怎么写?
下一个:怎样编程,vb语言是什么