GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Apr-2005, 15:59
mpviii mpviii is offline
New Member
 
Join Date: Apr 2005
Posts: 4
mpviii is on a distinguished road

Sending message to GUI thread that ISNT the main thread


I am trying to send a user defined message to a secondary thread in my dialog based MFC application. My secondary thread is a derivative of the CWinThread class and I am launching it using:

CPP / C++ / C Code:
CWinThread* m_pThread;
m_pThread = AfxBeginThread(RUNTIME_CLASS(CMyThread));

From my main GUI thread, I would like to send a message to this secondary thread, I am using:

CPP / C++ / C Code:
DWORD result;
SendMessageTimeout((HWND__*)m_pThread, WM_SONG_SELECTED, 0, 0, ABORTIFHUNG |SMTO_NORMAL, 2000, &result));

My problem however is that when I do this, I get an error code 1400: invalid window handle.

How would I go about getting a valid handle to my thread class?

Is there an easier / better way to accomplish this?

The ultimate reason I am doing this is to send a CString to my second thread when a button is clicked in my main GUI thread.

Thanks in advance for the help, I am very new to MFC / VC++
-Mike Visconti
Last edited by LuciWiz : 19-Apr-2005 at 23:23. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 20-Apr-2005, 12:49
imbtf imbtf is offline
Junior Member
 
Join Date: Aug 2004
Posts: 41
imbtf will become famous soon enough
A (despite its name) "user interface thread" does not have a window associated with it by default. If you want to use any function that requires a window handle (HWND) you need to create a window (can be invisible) first.

I much prefer to use PostThreadMessage, it takes a thread id as destination. The m_nThreadID member of the CWinThread class serves well for this purpose. Use ON_THREAD_MESSAGE macro in your message map to catch the messages.

e.g
CPP / C++ / C Code:
PostThreadMessage(m_pThread->m_nThreadID, WM_SONG_SELECTED, WPARAM, LPARAM);
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Another problem dealing with main() and driver function tommy69 C Programming Language 4 20-Mar-2004 19:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:04.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.