Visual Studio 2010 hangs when I debug method AttachThreadInput()

旧街凉风 提交于 2019-11-27 06:24:18

问题


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

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

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

What could be the problem?


回答1:


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 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".

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.

See also: I warned you: The dangers of attaching input queues.



来源:https://stackoverflow.com/questions/8075568/visual-studio-2010-hangs-when-i-debug-method-attachthreadinput

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!