Solved

Thread with 500 points and Grade A

Posted on 2007-03-22
5
186 Views
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);

 trd->Start();

}

Outside the button handler.......

private: void repeat()
{      
      FILE *fd;

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

            
      do
      {
      
        memset(buffer,NULL,sizeof(buffer));

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

        Sleep(1000);

        fprintf(fd,"\n\n The value of iResult is %s \n\n ",buffer);
      }while(iResult!=-1);

}

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.........
0
Comment
Question by:tatikor_143
  • 3
  • 2
5 Comments
 
LVL 11

Expert Comment

by:DeepuAbrahamK
Comment Utility
Inside the thread do a postmessage() to the button handler using the valid window handle. That will solve your problem.
Best Regards,
DeepuAbrahamK
0
 

Author Comment

by:tatikor_143
Comment Utility
Can U post some sample code .........

Its highly appreciated.....
0
 
LVL 11

Accepted Solution

by:
DeepuAbrahamK earned 500 total points
Comment Utility
//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
DWORD WINAPI MyThreadProc(LPVOID lpVoid)
{

// 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

BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
//{{AFX_MSG_MAP(CMyDialog)
ON_MESSAGE(WM_MYBUTTON,OnMyButtonPress)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

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

Author Comment

by:tatikor_143
Comment Utility
Hi,

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...............

Thanks


0
 

Author Comment

by:tatikor_143
Comment Utility
Fixed......
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now