re = WaitForSingleObject(ProcHandle,100);
Application->ProcessMessage();//这句很重要,向系统发送消息,不至于系统当机
这时C++程序段,使用了WaitForSingleObject函数后,调用了ProcessMessage函数,有资料说是如不调用ProcessMessage函数,系统将只运行这段程序而不会运行其他程序了.
现在问题是在VB中调用了WaitForSingleObject后CPU占用100%,请问该怎么解决呢?

解决方案 »

  1.   

    试试这个例子:
    'This program needs a common dialog box, named CDBoxConst INFINITE = &HFFFF
    Const STARTF_USESHOWWINDOW = &H1
    Private Enum enSW
        SW_HIDE = 0
        SW_NORMAL = 1
        SW_MAXIMIZE = 3
        SW_MINIMIZE = 6
    End Enum
    Private Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
    End Type
    Private Type STARTUPINFO
        cb As Long
        lpReserved As String
        lpDesktop As String
        lpTitle As String
        dwX As Long
        dwY As Long
        dwXSize As Long
        dwYSize As Long
        dwXCountChars As Long
        dwYCountChars As Long
        dwFillAttribute As Long
        dwFlags As Long
        wShowWindow As Integer
        cbReserved2 As Integer
        lpReserved2 As Byte
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
    End Type
    Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
    End Type
    Private Enum enPriority_Class
        NORMAL_PRIORITY_CLASS = &H20
        IDLE_PRIORITY_CLASS = &H40
        HIGH_PRIORITY_CLASS = &H80
    End Enum
    Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    Private Function SuperShell(ByVal App As String, ByVal WorkDir As String, dwMilliseconds As Long, ByVal start_size As enSW, ByVal Priority_Class As enPriority_Class) As Boolean
        Dim pclass As Long
        Dim sinfo As STARTUPINFO
        Dim pinfo As PROCESS_INFORMATION
        'Not used, but needed
        Dim sec1 As SECURITY_ATTRIBUTES
        Dim sec2 As SECURITY_ATTRIBUTES
        'Set the structure size
        sec1.nLength = Len(sec1)
        sec2.nLength = Len(sec2)
        sinfo.cb = Len(sinfo)
        'Set the flags
        sinfo.dwFlags = STARTF_USESHOWWINDOW
        'Set the window's startup position
        sinfo.wShowWindow = start_size
        'Set the priority class
        pclass = Priority_Class
        'Start the program
        If CreateProcess(vbNullString, App, sec1, sec2, False, pclass, _
        0&, WorkDir, sinfo, pinfo) Then
            'Wait
            WaitForSingleObject pinfo.hProcess, dwMilliseconds
            SuperShell = True
        Else
            SuperShell = False
        End If
    End Function
    Private Sub Form_Load()
        'Set the dialog's title
        CDBox.DialogTitle = "Choose an EXEC-File ..."
        'Error when canceled
        CDBox.CancelError = True
        'Set the dialog's filter
        CDBox.Filter = "EXEC-Files (*.exe)|*.exe|All files (*.*)|*.*"
        'Show the 'Open File'-dialog
        CDBox.ShowOpen
        'Execute the program
        SuperShell CDBox.FileName, Left$(CDBox.FileName, Len(CDBox.FileName) - Len(CDBox.FileTitle)), 0, SW_NORMAL, HIGH_PRIORITY_CLASS
        End
    End Sub
      

  2.   

    感谢 ch21st(风尘鸟) 的回答,但是没有解决我的问题,我说详细一点吧:
    Do While re <> 0
            re = WaitForSingleObject(ProcHandle, 10000)
    Loop
    其中ProcHandle是个监视指定文件夹是否改变的进程,这个进程用FindFirstChangeNotification创建,这个进程在指定的文件夹得到改变之后返回
    即我的目的是检测指定的文件夹是否发生变化,结果WaitForSingleObject将这个进程阻塞了,系统不能运行别的任何程序。我的理解是ProcHandle结束时WaitForSingleObject返回0,因此我判断re=0时退出循环,程序实际运行当中发现re一直为-1,是不是我的理解有错误呀
      

  3.   

    呵呵,WaitForSingleObject用在进程和线程时它的作用是等待进程和线程的关闭
    只要你的进程没有关闭WaitForSingleObject就会根据你设的时间等待下去Return Values
    If the function succeeds, the return value indicates the event that caused the function to return. This value can be one of the following. Value Meaning 
    WAIT_ABANDONED The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled. 
    WAIT_OBJECT_0 The state of the specified object is signaled. 
    WAIT_TIMEOUT The time-out interval elapsed, and the object's state is nonsignaled. 
    这是WaitForSingleObject的三种返回值