Board index » cppbuilder » Unable to suspend a thread from a Form->buttonclick event

Unable to suspend a thread from a Form->buttonclick event


2004-02-12 07:36:42 PM
cppbuilder63
I want to suspend a thread when the user clicks on Form Button, but it seems
not to work. There is an Eaccess violation message when i try this command
Nethread->Suspended=true.
inside the Button click event.
Please help
cheers,
Ronan
 
 

Re:Unable to suspend a thread from a Form->buttonclick event

"Bhuvan Sharma" < XXXX@XXXXX.COM >wrote in message
Quote
There is an Eaccess violation message when i try this command
Nethread->Suspended=true.
inside the Button click event.
An AV occurs when you access invalid memory. Please show your actual code.
Chances are, your pointer to the thread is not set up correctly in the first
place.
Gambit
 

Re:Unable to suspend a thread from a Form->buttonclick event

Quote
An AV occurs when you access invalid memory. Please show your actual
code.
Chances are, your pointer to the thread is not set up correctly in the
first
place.
below is my code snippet. Actually the Button2click event creates a new
application thread, and button3click event should resume or suspend the
thread based on global variable Issuspended value.
=============================================
bool Issuspended;
__fastcall AppThread::AppThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
Priority=tpLower;
FreeOnTerminate=true;
ADODataSet3 = new TADODataSet(NULL);
ADOConnection3 = new TADOConnection(NULL);
this->ADOConnection3->ConnectionString="Provider=MSDAORA.1;User
ID=bhuvan;Data Source=CCDDEV;Persist Security Info=False";
this->ADOConnection3->Connected=true;
this->ADODataSet3->Connection=this->ADOConnection3;
}
void __fastcall AppThread::Execute()
{
int i;
SetName();
int track=Form1->TrackBar1->Position;
CoInitialize(NULL);
this->run_the_application(); //basically calls the other thread
functions that do the database processing
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//creates an application thread Newthread
AppThread *Newthread= new AppThread(false);
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if(Issuspended)
{ Issuspended=false;
Newthread->Resume();
Form1->Button3->Caption="Pause";
}
else
{
Issuspended=true;
Newthread->Suspended=true;
// or i culd have used Newthread->Suspend(); as well but it also
returns the AV error
Form1->Button3->Caption="Resume";
}
}
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Bhuvan Sharma" < XXXX@XXXXX.COM >wrote in message
news:402b654f$ XXXX@XXXXX.COM ...

>There is an Eaccess violation message when i try this command
>Nethread->Suspended=true.
>inside the Button click event.

An AV occurs when you access invalid memory. Please show your actual
code.
Chances are, your pointer to the thread is not set up correctly in the
first
place.


Gambit


 

{smallsort}

Re:Unable to suspend a thread from a Form->buttonclick event

"Bhuvan Sharma" < XXXX@XXXXX.COM >wrote in message
Quote
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//creates an application thread Newthread
AppThread *Newthread= new AppThread(false);
}
You are assigning the new thread instance to a local pointer, not a global
pointer. The local pointer will go out of scope when the event handler
returns and you will no longer have any access to the thread.
Quote
void __fastcall TForm1::Button3Click(TObject *Sender)
{
<snip>
Newthread->Suspended=true;
You are trying to access a thread instance via a pointer that has not
actually been assigned to anything. That is why you get an AV.
Gambit
 

Re:Unable to suspend a thread from a Form->buttonclick event

Great, it works!!
thanks a ton
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Bhuvan Sharma" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>void __fastcall TForm1::Button2Click(TObject *Sender)
>{
>//creates an application thread Newthread
>AppThread *Newthread= new AppThread(false);
>}

You are assigning the new thread instance to a local pointer, not a global
pointer. The local pointer will go out of scope when the event handler
returns and you will no longer have any access to the thread.

>void __fastcall TForm1::Button3Click(TObject *Sender)
>{
<snip>
>Newthread->Suspended=true;

You are trying to access a thread instance via a pointer that has not
actually been assigned to anything. That is why you get an AV.


Gambit