3

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?

5
  • First of all, you don't check the return codes, the AttachThreadInput may fail and you should always check that. Secondly, my guess is that VS2010 (which thread you will attach to your thread) does something that's not compatible with this call or with your input event handling. Nov 10, 2011 at 6:25
  • I can't get return value after calling AttachThreadInput, VS is hangs...
    – Swan911
    Nov 10, 2011 at 7:21
  • Regarding the latter, most likely it is. So I ask what to do?
    – Swan911
    Nov 10, 2011 at 7:23
  • You have to think about what you are trying to accomplish, and see if there is another way to do it. Nov 10, 2011 at 7:25
  • I did so. I excluded the code for debug mode(#ifndef _DEBUG). But the problem is not resolved!
    – Swan911
    Nov 10, 2011 at 8:38

1 Answer 1

34

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.

3
  • 2
    This is an excess of metaphors. It barely explains the problem. It looks like storytelling.
    – GetFree
    Jul 20, 2017 at 17:01
  • @Raymond Chen I have read a pile of material on why AttachThread can hang, and yet many people still use (like me, using voice input to Process.Start/ShellExecute to open/bring apps/windows to the foreground) to solve legitimate user-friendly needs involving one app bringing another app to the foreground and giving it focus. I wish there was some official, approved, documented (with examples) way of doing that so we all didn't have to use the AttachThread hack.
    – Kevin
    Feb 26, 2020 at 15:13
  • 1
    @Kevin I think if you declare your process to be UIAccess, then that grants blanket permission to steal foreground. Feb 26, 2020 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.