Click here to Skip to main content
15,888,733 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to call PostThreadMessage from outside the thread function Pin
saha2k51-Mar-05 19:39
saha2k51-Mar-05 19:39 
AnswerRe: I got the answer !!!!!!!!!! Pin
saha2k51-Mar-05 22:10
saha2k51-Mar-05 22:10 
GeneralNULL character problem Pin
LighthouseJ1-Mar-05 18:38
LighthouseJ1-Mar-05 18:38 
GeneralRe: NULL character problem Pin
RicoH2-Mar-05 0:31
RicoH2-Mar-05 0:31 
GeneralRe: NULL character problem Pin
LighthouseJ2-Mar-05 2:48
LighthouseJ2-Mar-05 2:48 
GeneralRe: NULL character problem Pin
David Crow2-Mar-05 2:34
David Crow2-Mar-05 2:34 
GeneralRe: NULL character problem Pin
LighthouseJ2-Mar-05 2:46
LighthouseJ2-Mar-05 2:46 
QuestionHow to call PostThreadMessage from outside the thread function Pin
saha2k51-Mar-05 18:23
saha2k51-Mar-05 18:23 
I am experiencing problem regarding calling a "PostThreadMessage" function from outside the thread function. A code is attched below... Please go through it and guide me as to how to go about correcting it...

/*
The PRODUCER and OTHER threads post messages to the
CONSUMER thread. All the threads wait on the
"consumerReady" event to be set by the main thread.
*/

#include <windows.h>
#include <iostream.h>
#include <stdio.h>

typedef struct temp
{
int msid;
char *msg;
}box;

// Thread IDs. Define these globally so the threads can refer to each other.

DWORD producerID, consumerID, otherID;

void get_msg(DWORD quid,box *obj);
void post_msg(DWORD quid,box *obj);

// An event
HANDLE consumerReady;


// The producer thread.

DWORD WINAPI produce(LPVOID)
{

// Do not start producing until all threads are ready.
WaitForSingleObject(consumerReady, INFINITE);


while(1)
{
box *ptr;
ptr=(box *)malloc(sizeof(box));
ptr->msg = (char *)malloc(10);
strcpy(ptr->msg,"hello\0");
ptr->msid = 12;
post_msg(consumerID,ptr);
Sleep(1000);
}

return 0;
}

// The consumer thread.

DWORD WINAPI consume(LPVOID)
{
// Do not start producing until all threads are ready.
WaitForSingleObject(consumerReady, INFINITE);
Sleep(0);

while (true)
{
box *ptr;
ptr=(box *)malloc(sizeof(box));
ptr->msg = (char *)malloc(10);
get_msg(consumerID,ptr);
}
return 0;
}

DWORD WINAPI other(LPVOID)
{

// Do not start producing until all threads are ready.
WaitForSingleObject(consumerReady, INFINITE);

// Send simple integers. Use WM_USER messages with the integer values
// in the wParam field.
while(1)
{
box *ptr;
ptr=(box *)malloc(sizeof(box));
ptr->msg = (char *)malloc(10);
strcpy(ptr->msg,"other\0");
ptr->msid = 11;
Sleep(1000);
post_msg(consumerID,ptr);
}

return 0;
}

// main() runs the simulation.

int main()
{
// Create an event which is initially unsignaled
consumerReady = CreateEvent(0, TRUE, FALSE, 0);

// Create the threads and start them immediately.
HANDLE producerHandle = CreateThread(0, 0, produce, 0, 0, &producerID);
HANDLE consumerHandle = CreateThread(0, 0, consume, 0, 0, &consumerID);
HANDLE otherHandle = CreateThread(0, 0, other, 0, 0, &otherID);

//Set the event now that all threads are ready
SetEvent(consumerReady);

//Keep the main thread alive
WaitForSingleObject(producerHandle, INFINITE);
WaitForSingleObject(consumerHandle, INFINITE);

return 0;
}

void post_msg(DWORD quid,box *obj)
{
PostThreadMessage(quid, WM_USER, (WPARAM)obj, 0);
int err = GetLastError();
printf("\n obj_post->msgid %d ",obj->msid);
printf("\n err_post = %d ",err);
}

void get_msg(DWORD quid,box *obj)
{
MSG lmsg;
GetMessage(&lmsg, 0, 0, 0);
obj = (box*)(lmsg.wParam);
int err = GetLastError();
printf("\n obj_get->msgid %d ",obj->msid);
printf("\n obj_get->msg %c ",*(obj->msg));
printf("\n err_get = %d ",err);
}


The output is
obj_post->msgid 12
err_post = 1444
obj_get->msgid 12
obj_get->msg h
err_get = 0
obj_get->msgid 11
obj_get->msg o
err_get = 0
obj_post->msgid 11
err_post = 0
obj_post->msgid 12
err_post = 1444
obj_get->msgid 11
obj_get->msg o
err_get = 0
obj_get->msgid 12
obj_get->msg h
err_get = 0

As can be seen in some cases, posting the messages results in a error id of 1444(INVALID THREAD IDENTIFIER) which is puzzling cos all the threads are already created....

Any help would be appreciated in this regard...
AnswerRe: How to call PostThreadMessage from outside the thread function Pin
saha2k51-Mar-05 22:25
saha2k51-Mar-05 22:25 
Question&quot; MFC appliation has encountered an error&quot; Why??? Pin
wasife1-Mar-05 17:32
wasife1-Mar-05 17:32 
AnswerRe: &quot; MFC appliation has encountered an error&quot; Why??? Pin
Christian Graus1-Mar-05 18:04
protectorChristian Graus1-Mar-05 18:04 
AnswerRe: " MFC appliation has encountered an error" Why??? Pin
David Crow2-Mar-05 2:44
David Crow2-Mar-05 2:44 
GeneralRe: &quot; MFC appliation has encountered an error&quot; Why??? Pin
Chris Losinger2-Mar-05 3:16
professionalChris Losinger2-Mar-05 3:16 
QuestionHow can I get the MAC addresses of all IP devices on same network ? Pin
charisma_youn1-Mar-05 16:34
charisma_youn1-Mar-05 16:34 
AnswerRe: How can I get the MAC addresses of all IP devices on same network ? Pin
Alexander M.,2-Mar-05 2:58
Alexander M.,2-Mar-05 2:58 
Questionhow to create setup files Pin
rjnl1-Mar-05 15:39
rjnl1-Mar-05 15:39 
AnswerRe: how to create setup files Pin
LighthouseJ1-Mar-05 15:59
LighthouseJ1-Mar-05 15:59 
AnswerRe: how to create setup files Pin
zhang8006051-Mar-05 16:13
zhang8006051-Mar-05 16:13 
AnswerRe: how to create setup files Pin
ThatsAlok2-Mar-05 2:38
ThatsAlok2-Mar-05 2:38 
QuestionHow to determine the column No. in CListView when user right-clicked the list header? Pin
lisoft1-Mar-05 15:32
lisoft1-Mar-05 15:32 
AnswerRe: How to determine the column No. in CListView when user right-clicked the list header? Pin
zhang8006051-Mar-05 17:04
zhang8006051-Mar-05 17:04 
GeneralSeveral child documents Pin
joy0071-Mar-05 15:32
joy0071-Mar-05 15:32 
GeneralPointer to Void problem Pin
LighthouseJ1-Mar-05 15:19
LighthouseJ1-Mar-05 15:19 
GeneralRe: Pointer to Void problem Pin
zhang8006051-Mar-05 15:44
zhang8006051-Mar-05 15:44 
GeneralRe: Pointer to Void problem Pin
LighthouseJ1-Mar-05 16:17
LighthouseJ1-Mar-05 16:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.