CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2003
    Location
    M...N
    Posts
    220

    [Quiz] What will happen if SendMessage to self?

    This is an interesting thing (what i never thought of yet...) and I wonder how you people think it will happen.

    We normally won't sendmessage to ourself, but what if we do that?

    Here are the sample example:
    Code:
    LRESULT CDlgExeDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	switch (message)
    	{
    	case WM_MYTEST:
    		{
    			if (! SendMessage(WM_MYTEST)) {
    				ASSERT(0);
    			};
    			
    			int i = 0;
    
    		}
    .....
    Because SendMessage is a synchronous function, so THEORETICALLY it will casue an infinite loop!

    But is this true?

    How do you people think?

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: [Quiz] What will happen if SendMessage to self?

    Quote Originally Posted by myron
    ...Because SendMessage is a synchronous function, so THEORETICALLY it will casue an infinite loop!
    Yes it will, and then - stack overlow...
    Did you try?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    Sep 2003
    Location
    M...N
    Posts
    220

    Re: [Quiz] What will happen if SendMessage to self?

    m. but actually (usiang VC6)

    it just runs!
    (After a certain number of stack-record repetition...)
    A very interesting things...

    Says, what if we repalce WM_MYTEST with WM_CREATE?

    The dialog/app still shows up!

    What a magic.

  4. #4
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: [Quiz] What will happen if SendMessage to self?

    Quote Originally Posted by myron
    We normally won't sendmessage to ourself, but what if we do that?
    We normally don't send messages recursively to ourself (i.e. send WM_MYMESSAGE inside a WM_MYMESSAGE handler), but other than that its very normal to send messages to ourself.

  5. #5
    Join Date
    Sep 2003
    Location
    M...N
    Posts
    220

    Re: [Quiz] What will happen if SendMessage to self?

    Well, there is a trcik here.
    Sendmessage is synchronous function, so "theoretically" this will cause system hang out... since the sendmessage won't return back... (in single thread condition of course)

    ...
    case WM_MYTEST:
    {
    if (! SendMessage(WM_SOMEOTHERMSG)) {
    ASSERT(0);
    };
    ...

  6. #6
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: [Quiz] What will happen if SendMessage to self?

    it's the first sign of madness

  7. #7
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: [Quiz] What will happen if SendMessage to self?

    Quote Originally Posted by myron
    Well, there is a trcik here.
    Sendmessage is synchronous function, so "theoretically" this will cause system hang out... since the sendmessage won't return back... (in single thread condition of course)
    Unless I'm mistaken it won't necesarily hang...first execution is in the case WM_MYTEST code, then when you call SendMessage(WM_SOMETHINGELSE), execution goes to the WM_SOMETHINGELSE handler. When that handler completes, execution returns to the WM_MYTEST handler, after the SendMessage call.

    Now, if we're talking about Windows messages, not user defined messages, it may or may not cause problems, depending on the message & what Windows does with it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured