Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

c++ - Visual Studio 2010 hangs when I debug method AttachThreadInput()

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...