Board index » cppbuilder » thread issue

thread issue


2007-06-19 07:04:29 AM
cppbuilder71
hello im new in threading and i have a problem, i have created a thread in
one form
. the thread is very simple:
class TTestThread : public TThread
{
__published: // IDE-managed Components
void __fastcall Execute();
void __fastcall Dosomething();
void __fastcall Dosmethingelse();
private: // User declarations
public: // User declarations
__fastcall TTestThread ();
};
the thread is started when the user clicks a button in the form, and the
Doscomething() function runs and ends when
it shows a result to an edit box.
i dont terminate the thread at that point. the user then clicks another
button and moves to another form.
can i then call the same thread from the new form and continue and do the
Dosomethingelse()?
i mean how can i use one thread from one form to another?
or is it better to terminate the thread and create a new one from the other
form?thx
 
 

Re:thread issue

"abc" < XXXX@XXXXX.COM >wrote:
Quote

Almost an entire week without an answer - gee guys.
Quote
[...] the thread is started when the user clicks a button in
the form, and the Doscomething() function runs and ends when
it shows a result to an edit box.
You're not gaining anything because DoSomething is called from
the main thread and therefore executing within the context of
the main thread - not the worker thread.
Quote
i dont terminate the thread at that point. the user then
clicks another button and moves to another form. can i then
call the same thread from the new form and continue and do
the Dosomethingelse()?
Not the way that you have it now, no. However, it is possible.
Quote
i mean how can i use one thread from one form to another?
Assuming that you want the thread to live for the duration
of your application, then allocate it in the main form's
constructor and destroy it in the main form's destructor.
For example:
//-------------------------------------------------------------
class TMyThread; // forward declare
//-------------------------------------------------------------
class TForm1 : public TForm
{
private:
TMyThread *MyThread;
public:
__fastcall TForm1(TComponent* Owner);
__fastcall ~TForm1(); // <--- destructor
};
//-------------------------------------------------------------
#include "MyThread.h" // <-- or what ever you named it
//-------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
MyThread = new TMyThread( this );
}
//-------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
MyThread->Terminate();
MyThread->WaitFor();
delete MyThread;
}
//-------------------------------------------------------------
Simple enough no?
Just note that the above assumes that the TThread's Execute
periodically checks it's Terminated property and if true, it
gracefully exits. For example:
//-------------------------------------------------------------
void __fastcall TMyThread::Execute()
{
while( SomeCondition && ! Terminated )
{
if( ! Terminated )
{
DoSomeThing();
}
if( ! Terminated )
{
DoAnotherThing();
}
}
}
//-------------------------------------------------------------
In reality though, for your purposes, the Execute method looks
a bit different. All you have to do is get the thread to create
a message queue and then you issue it commands via PostThreadMessage.
For example:
#define UWM_FUNCTION1 WM_USER + 100
#define UWM_FUNCTION2 WM_USER + 101
#define UWM_FUNCTION3 WM_USER + 102
//-------------------------------------------------------------
void __fastcall TMyThread::Execute()
{
MSG msg;
// create a message queue
PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
// wait for messages
while( !Terminated && ::GetMessage(&msg, NULL, 0, 0)>0 )
{
switch( msg.message )
{
case UWM_FUNCTION1: // code for function1
break;
case UWM_FUNCTION2: // code for function2
break;
case UWM_FUNCTION3: // code for function3
break;
}
}
}
//-------------------------------------------------------------
void __fastcall TMyThread::DoFunction1()
{
PostThreadMessage(ThreadID, UWM_FUNCTION1, 0, 0);
}
//-------------------------------------------------------------
void __fastcall TMyThread::DoFunction2()
{
PostThreadMessage(ThreadID, UWM_FUNCTION2, 0, 0);
}
//-------------------------------------------------------------
void __fastcall TMyThread::DoFunction3()
{
PostThreadMessage(ThreadID, UWM_FUNCTION3, 0, 0);
}
//-------------------------------------------------------------
void __fastcall TMyThread::TerminateThread()
{
Terminate();
PostThreadMessage(ThreadID, WM_QUIT, 0, 0);
}
//-------------------------------------------------------------
~ JD