2010-03-31 74 views
5

Để thử cách lập trình với API Win32, tôi đã viết một chương trình tạo ra một quy trình. Sau đó, tôi muốn kiểm tra xem quá trình tôi chờ đợi cho quá trình mới được tạo ra, đóng tay cầm và sau đó kiểm tra WaitForSingleObject một lần nữa (quá trình thứ hai đang ngủ với giá 700   ms)Cách sử dụng WaitForSingleObject

quá trình đầu tiên:

#include <iostream> 
#include <windows.h> 
#include <string> 

using namespace std; 

void main() 
{ 
    bool ret; 
    bool retwait; 

    STARTUPINFO startupinfo; 
    GetStartupInfo (&startupinfo); 

    PROCESS_INFORMATION pro2info; 

    wchar_t wcsCommandLine[] = L"D:\\betriebssystemePRA1PRO2.exe"; 

    ret = CreateProcess(NULL, wcsCommandLine, NULL, NULL, false, CREATE_NEW_CONSOLE, NULL, 
         NULL, &startupinfo, &pro2info); 

    cout<<"hProcess: "<<pro2info.hProcess<<endl; 
    cout<<"dwProcessId: "<<pro2info.dwProcessId <<endl; 

    if (retwait= WaitForSingleObject (pro2info.hProcess, INFINITE)==true) 
     cout<<"waitprocess:true"<<endl; //The process is finished 
    else 
     cout<<"waitprocess:false"<<endl; 

    CloseHandle (pro2info.hProcess);//prozesshandle schließen, "verliert connection" 

    if (retwait= WaitForSingleObject (pro2info.hProcess, INFINITE)==true) //When the process has finished 
     cout<<"waitprocess:true"<<endl; 
    else 
     cout<<"waitprocess:false"<<endl; 

    //cout<<GetLastError()<<endl; //Output the last error. 

    ExitProcess(0); 
} 

Second quy trình:

#include <iostream> 
#include <windows.h> 
#include <string> 

using namespace std; 

void main() 
{ 
    int b; 

    b = GetCurrentProcessId(); 

    cout << b << endl; 
    cout << "Druecken Sie Enter zum Beenden" << endl; 
    cin.get(); 
     //Wait until the user confirms 

    Sleep (700); 
    ExitProcess(0); 

    cout<<"test"; 
} 

Quy trình đầu tiên in sai, sai; nhưng nó phải in đúng, sai.

Thay vì tuyên bố if-else, tôi sử dụng này:

//switch(WaitForSingleObject (pro2info.hProcess, INFINITE)){ 
    // case WAIT_OBJECT_0: cout << "ja"; 
    //  break; 
    // case WAIT_FAILED:cout << "nein"; 
    //  break; 
    // case WAIT_TIMEOUT: 
    //  break; 
    //} 
// cout<<"waitprocess:true"<<endl;//prozess ist fertig 
//else 
// cout<<"waitprocess:false"<<endl; 

Và điều này dường như để làm việc. Tôi đã làm gì sai với tuyên bố if-else của mình?

Trả lời

16

Bạn thực sự cần phải chú ý đến ý nghĩa cho giá trị trả về của các hàm API. Bạn không thể bỏ qua một FALSE trả về từ CreateProcess(). WaitForSingleObject() có thể trả về một vài giá trị, nó trả về 0 nếu chờ đợi hoàn tất thành công. Mà làm cho bạn in "sai".

5

Theo MSDN, WaitForSingleObject sẽ trả lại WAIT_OBJECT_0 nếu chờ đợi không bị hủy. Nếu bạn kiểm tra tài liệu, giá trị của WAIT_OBJECT_0 xảy ra là 0x00000000L, điều này xảy ra là giá trị thường được chuyển đổi thành false, không phải là true. Do đó so sánh của bạn không thành công.

Quảng bá giá trị trả lại là WaitForSingleObject đến bool là IMHO không phải là ý tưởng hay vì bạn nhận được một số mã trả về khác không có khả năng chiếu sáng cho biết lý do chờ đợi hết hạn.

Nếu bạn vẫn muốn giữ mã ở trên bằng cách sử dụng séc boolean, thay đổi các thử nghiệm thành !WaitForSingleObject(...) thay thế.

2

Tôi nghĩ bạn đã tự trả lời câu hỏi của mình. Vấn đề là WaitForSingleObject không trả lại true hoặc false, nhưng WAIT_OBJECT_0 et al.

Vì vậy, thay vì

if (retwait= WaitForSingleObject (pro2info.hProcess, INFINITE)==true) 

bạn cần

if (retwait= WaitForSingleObject (pro2info.hProcess, INFINITE)==WAIT_OBJECT_0)