Thread with 500 points and Grade A

Posted on 2007-03-22
Last Modified: 2013-11-26
I am a server listening to the client and receiving the message from the client periodically. This is under one thread.
Actually the situation is........

When I click on the button ....The event handler in the button creates a new thread for me and handles the messages that I receive from the client and in the Button handler I need to Paint the form accordingly as per the  messages received from the client.

Here is the code......

 public: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

ThreadStart ^myThreadDelegate = gcnew ThreadStart(this, &CppDraw::Form1::repeat);
                                     trd = gcnew Thread(myThreadDelegate);



Outside the button handler.......

private: void repeat()
      FILE *fd;

    fd = fopen("rd_log1.txt","w");


        iResult = recv(s,buffer,256,NULL);


        fprintf(fd,"\n\n The value of iResult is %s \n\n ",buffer);


Here my issue is when I receive the message from the client inside the thread I want to pass this data to the clent and paint the form accordingly.....

Any idea on how to do this.........
Question by:tatikor_143
  • 3
  • 2
LVL 11

Expert Comment

ID: 18777656
Inside the thread do a postmessage() to the button handler using the valid window handle. That will solve your problem.
Best Regards,

Author Comment

ID: 18779861
Can U post some sample code .........

Its highly appreciated.....
LVL 11

Accepted Solution

DeepuAbrahamK earned 500 total points
ID: 18784732
//Calling a thread
hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)MyThreadProc,this,0,NULL); // 'this' is the class object from which you call the thread.

//Thread proc

// CMyClass* pMyclass = reinterpret_cast<CMyClass*>(lpVoid); assume that you are passing CMyclass object.
// pMyClass-> Call any funtion...if you wish...
//pass a valid handle to the window in which you want to post the message...pMyclass->m_hWnd
 ::PostMessage(pMyclass->m_hWnd, WM_MYBUTTON, (WPARAM)0,(LPARAM)0);


In you dialog class header

#define WM_MYBUTTON      WM_USER+1


Hope this will give you the may need to tweak it a bit to make it work on your program..
Please shout if you need more help...

Best Regards,
Its better to use PostMessage so the worker thread doesn't have to wait while the main thread processes.

Author Comment

ID: 18789902

I am using WM_COPYDATA to resolve the above issue . I am able to send the data but not able to receive it....

How to implement...............



Author Comment

ID: 18795922

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question