IsWindow(m_hwnd) returns 0.
-
IsWindow(m_hwnd) returns 0. Hi, guys: I've met a problem that a new thread tests the window handle m_hwnd through IsWindow() but reurns 0. below is related code: ///////////////////////////////////////////// class MyClass { public: HWND m_hwnd; ...... int run(); void * run_undetached(); }; void mainfunc() { ...... MyClass a; a.run(); } int MyClass::run() { ...... m_hwnd = CreateWindow(......); //start the run_undetached() in a new thread here. ...... } void * MyClass::run_undetached() { ...... if ( !IsWindow(m_hwnd)) outputmsg("m_hwnd is not a window!"); } ///////////////////////////////////////////// any comments? TIA
-
IsWindow(m_hwnd) returns 0. Hi, guys: I've met a problem that a new thread tests the window handle m_hwnd through IsWindow() but reurns 0. below is related code: ///////////////////////////////////////////// class MyClass { public: HWND m_hwnd; ...... int run(); void * run_undetached(); }; void mainfunc() { ...... MyClass a; a.run(); } int MyClass::run() { ...... m_hwnd = CreateWindow(......); //start the run_undetached() in a new thread here. ...... } void * MyClass::run_undetached() { ...... if ( !IsWindow(m_hwnd)) outputmsg("m_hwnd is not a window!"); } ///////////////////////////////////////////// any comments? TIA
The remarks in MSDN for IsWindow() actually say that a thread should not call IsWindow() for a window that it did not create because the window could be destroyed after the function is called - so that's not a promising start... Anyway, here's a couple of likely reasons for your problem: 1) Does the call to CreateWindow() succeed? If not, then obviously IsWindow() will not succeed either. 2) When does the window get destroyed? If it is destroyed in the MyClass destructor, then this will be called at the end of the scope of the MyClass object (since it is created on the stack). From the code snippet above, that means that it will be called when mainfunc() terminates, which, unless you have some sort of syncrhonisation, will be before your thread function terminates. Consequently, the window will be destroyed before the thread gets around to using it. Dave http://www.cloudsofheaven.org
-
IsWindow(m_hwnd) returns 0. Hi, guys: I've met a problem that a new thread tests the window handle m_hwnd through IsWindow() but reurns 0. below is related code: ///////////////////////////////////////////// class MyClass { public: HWND m_hwnd; ...... int run(); void * run_undetached(); }; void mainfunc() { ...... MyClass a; a.run(); } int MyClass::run() { ...... m_hwnd = CreateWindow(......); //start the run_undetached() in a new thread here. ...... } void * MyClass::run_undetached() { ...... if ( !IsWindow(m_hwnd)) outputmsg("m_hwnd is not a window!"); } ///////////////////////////////////////////// any comments? TIA
Greets, Yes, a comment I have is that there is not enough context; you can't see what run_undetached returns and it's not clear as to whether or not CreateWindow() returned null. What are you trying to do that I may have missed from your code? Regards, Joe
-
IsWindow(m_hwnd) returns 0. Hi, guys: I've met a problem that a new thread tests the window handle m_hwnd through IsWindow() but reurns 0. below is related code: ///////////////////////////////////////////// class MyClass { public: HWND m_hwnd; ...... int run(); void * run_undetached(); }; void mainfunc() { ...... MyClass a; a.run(); } int MyClass::run() { ...... m_hwnd = CreateWindow(......); //start the run_undetached() in a new thread here. ...... } void * MyClass::run_undetached() { ...... if ( !IsWindow(m_hwnd)) outputmsg("m_hwnd is not a window!"); } ///////////////////////////////////////////// any comments? TIA
MFC does not support access of CWnd's from threads other than the ui thread, which I think is what you are doing. There are two approaches you can take: 1. Pass the HWND only to the other thread, and have it use the Windows api directly. 2. Use ::PostMessage() from the other thread on that window handle and trap the message in you main ui thread, and process it there. (I would strongly recommend this approach, as you really shouldn't be accessing windows from threads other than the main ui thread (except to post window messages)). Cheers, Swine [b]yte your digital photos with [ae]phid [p]hotokeeper - www.aephid.com.
-
The remarks in MSDN for IsWindow() actually say that a thread should not call IsWindow() for a window that it did not create because the window could be destroyed after the function is called - so that's not a promising start... Anyway, here's a couple of likely reasons for your problem: 1) Does the call to CreateWindow() succeed? If not, then obviously IsWindow() will not succeed either. 2) When does the window get destroyed? If it is destroyed in the MyClass destructor, then this will be called at the end of the scope of the MyClass object (since it is created on the stack). From the code snippet above, that means that it will be called when mainfunc() terminates, which, unless you have some sort of syncrhonisation, will be before your thread function terminates. Consequently, the window will be destroyed before the thread gets around to using it. Dave http://www.cloudsofheaven.org
thanks for your reply ! :-) 1) CreateWindow succeeds clearly! 2) the window will be available till mainfunc() terminates. more help ?
-
thanks for your reply ! :-) 1) CreateWindow succeeds clearly! 2) the window will be available till mainfunc() terminates. more help ?
Well that's your problem then. When mainfunc() terminates, your class will be destroyed, and so will the window. Then your other thread will be calling IsWindow() on a random piece of memory (the HWND has been destroyed, and so now is just a random number), so it will not work. You need to ensure that your class is not destroyed for the lifetime of the other thread. Dave http://www.cloudsofheaven.org