当我调试方法 AttachThreadInput() 时,Visual Studio 2010 挂起

2022-01-12 00:00:00 debugging freeze c++ mfc visual-studio-2010

我有 Visual Studio 2010 Ultimate SP1,我的项目基于 MFC.

I have Visual Studio 2010 Ultimate SP1 and my project is based on MFC.

当我调试项目的下一个代码时,Visual Studio 挂起:

When I debuging next code of my project Visual Studio is hangs:

CWnd* _window = CWnd::FromHandle(_hwnd_);
if (_window) {
    DWORD nForeThread, nAppThread;

    nForeThread = ::GetWindowThreadProcessId(::GetForegroundWindow(), 0);
    nAppThread = GetCurrentThreadId();

    if (nForeThread != nAppThread)
    {
        AttachThreadInput(nForeThread, nAppThread, TRUE);
        _window->BringWindowToTop();
        _window->ShowWindow(SW_RESTORE);
        AttachThreadInput(nForeThread, nAppThread, FALSE);
    }
    else
    {
        _window->BringWindowToTop();
        _window->ShowWindow(SW_RESTORE);
    }
}

我在下一行有一个断点:

I have a breakpoint on the next line:

    AttachThreadInput(nForeThread, nAppThread, TRUE);

所以如果我按下 F10 或 F11 或 F5 按钮,VS 会立即挂起.

So if I push F10 or F11 or F5 button, then VS immediately hangs.

可能是什么问题?

推荐答案

我怀疑你只是从某个地方复制了这段代码而没有了解后果.这是 每个 Win32 程序员都应该知道的五件事我在 2005 年讨论过.

I get the suspicion that you just copied this code from somewhere without understanding the consequences. This is one of the Five Things Every Win32 Programmer Should Know I discussed back in 2005.

AttachThreadInput 意思是请把这两个线程里的钱全部拿出来放在一个共同的银行账户里,这样两个线程都不能取钱,除非两个线程一起去银行取钱.钱."

AttachThreadInput means "Please take all the money from these two threads and put them in a joint bank account, so that neither thread can withdraw any money unless both threads go to the bank together to withdraw the money."

如果两个线程已婚或同一个家庭成员或以其他方式通过共享银行帐户相互信任,这很好,并且还可以协调他们的行动,以便当其中一个需要钱时,它可以调用另一个然后说嘿,你能和我一起去银行吗?我需要一些钱."

This is great if the two threads are married or members of the same family or otherwise trust each other with a shared bank account, and also can coordinate their actions so that when one of them needs money, it can call the other one and say "Hey, could you come to the bank with me? I need some money."

在你的情况下,你说:请把我所有的钱和所有街上随便一个人的钱都存入一个联名银行账户,这样我和除非我们俩一起去银行取钱,否则其他人可以提取任何钱."

In your case, you said, "Please take all all my money and all the money from some random person walking down the street and put them into a joint bank account, so that neither I nor that other person can withdraw any money unless both of us go to the bank together to withdraw the money."

这对您和对方来说都是个坏主意,因为您现在无法提取任何资金,除非您设法与对方取得联系.但是因为你随便抓了一个走在街上的人,你不知道他的手机号码,所以你没有办法联系他说嘿,你能和我一起去银行吗?"那个随机的人现在可以访问你所有的银行账户信息.(而且你的钱和那个人的钱混在一起了――不再是你的钱"和那个人的钱"了.它只是一堆钱.)

This is a bad idea, both for you and for the other person, since you now cannot withdraw any money unless you manage to get in touch with the other person. But since you just grabbed a random person walking down the street, you don't know his cell phone number, so you have no way of contacting him to say "Hey, can you go down to the bank with me?" And that random person now has access to all your bank account information. (And your money and that other person's money got lumped together -- it's not "your money" and "that other person's money" any more. It's just one pile of money.)

将银行账户"改为输入队列",将钱"改为输入",将去银行"改为查询消息队列".

Change "bank account" to "input queue", "money" to "input", and "go to the bank" to "check the message queue".

在您的情况下,您所做的是抓住 Visual Studio 并说好的,我们现在有一个联名银行账户!"现在看看会发生什么:Visual Studio 无法提取任何资金,因为它需要您去银行.但是你不能去银行,因为你被破解了调试器.结果:没有人得到任何钱.

In your case, what you did was grab Visual Studio and say "Okay, we now have a joint bank account!" Now look what happens: Visual Studio cannot withdraw any money because it needs you to go to the bank. But you can't go to the bank because you're broken into the debugger. Result: Nobody gets any money.

您的设计存在根本缺陷.你需要重新审视你的情况.

Your design is fundamentally flawed. You need to re-examine your situation.

另请参阅:我警告过您:附加输入队列的危险.

相关文章