C++ 如何使用 WaitForSingleObject

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2554447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:53:49  来源:igfitidea点击:

How to use WaitForSingleObject

c++windows

提问by Tyzak

In order to try out how to program with the Win32 API, I wrote a program that creates a process. Then I want to check if my process waits for the newly created process, close the handle and then check WaitForSingleObject again (the second process is sleeping for 700 ms)

为了尝试如何使用 Win32 API 进行编程,我编写了一个创建进程的程序。然后我想检查我的进程是否等待新创建的进程,关闭句柄然后再次检查WaitForSingleObject(第二个进程睡眠700毫秒)

First process:

第一个过程:

#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 process:

第二个过程:

#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";
}

The first process prints false, false ; but it should print true, false.

第一个过程打印 false, false ;但它应该打印真,假。

Instead of the if-else statement, I used this:

我使用了这个,而不是 if-else 语句:

//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;

And this seems to work. What did I do wrong with my if-elsestatement?

这似乎有效。我的if-else陈述有什么问题?

回答by Hans Passant

You reallyneed to pay attention to the meaning for the return value of the API functions. You cannot ignore a FALSE return from CreateProcess(). WaitForSingleObject() can return several values, it returns 0 if the wait completed successfully. Which makes you print "false".

确实需要注意 API 函数的返回值的含义。您不能忽略来自 CreateProcess() 的 FALSE 返回。WaitForSingleObject() 可以返回多个值,如果等待成功完成则返回 0。这让你打印“假”。

回答by Timo Geusch

According to MSDN, WaitForSingleObjectwill return WAIT_OBJECT_0 if the wait wasn't aborted. If you check the documentation, the value of WAIT_OBJECT_0 happens to be 0x00000000L, which happens to be the value commonly converted to false, not true. Hence your comparison fails.

根据MSDN,如果等待未中止,WaitForSingleObject将返回 WAIT_OBJECT_0。如果您查看文档,WAIT_OBJECT_0 的值恰好是 0x00000000L,这恰好是通常转换为 的值false,而不是true。因此你的比较失败了。

Promoting the return value of WaitForSingleObjectto a boolis IMHO not a good idea given that you get several potentially illuminating non-zero return codes that indicate why the wait expired.

WaitForSingleObject将 a的返回值提升bool为 IMHO 不是一个好主意,因为您会得到几个可能有启发性的非零返回代码,这些代码表明等待到期的原因。

If you still want to keep the above code as using a boolean check, change the tests to !WaitForSingleObject(...)instead.

如果您仍希望将上述代码保留为使用布尔检查,请将测试更改为!WaitForSingleObject(...)

回答by jpalecek

I think you sort of answered your question yourself. The point is that WaitForSingleObjectdoesn't return trueor false, but WAIT_OBJECT_0et al.

我想你自己回答了你的问题。关键是WaitForSingleObject不返回trueor false,而是WAIT_OBJECT_0et al。

So instead of

所以代替

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

you need

你需要

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