Board index » delphi » Using Threads and Application.processMessage to Cancel operationof main thread.

Using Threads and Application.processMessage to Cancel operationof main thread.

HI,
I have a problem with using threads.

Problem :
How can I use VCLs main thread to listen to windows messages posted in
message queue of a child thread.

I need to run a main form in which I perform repeated database access.
Mine is a three tier application hence the data-access may be time
consuming.
I want to provide a cancel button, using which the user may cancel current
activity.

Basically I will use a GlobalFlag which will be set once the cancel button
is pressed.
Each function in my main form, checks for the value of this flag before
proceeding.

My problem is that I am not able to trap the cancel button click.
I tried creating a separate child thread with Execute method looking like  :
TCancelThread.Execute;
begin
  while(True) do
     Application.ProcessMessages;
end;
but it does not trap.
I also tried to create a separate form for cancel button. I showed it with
formStyle : fsStayOnTop, and disabled the main form. still it did not trap
the message.
How can I trap ONLY Cancel Button Click while my main thread continues it's
work.

Rgds
Amit

 

Re:Using Threads and Application.processMessage to Cancel operationof main thread.


VCL message loop runs in main thread. This means that you have to leave main
thread to process user messages and create additional "working" threads:
procedure TWorkThread.Execute;
begin
  while Have_Some_Job and not Canceled do
    {do your job}
end;

procedure Txx.CancelBtnClick();
begin
  Canceled := true;
end;

procedure Txx.StartBtnClick();
begin
  TWorkThread.Create(false);
  CancelBtn.Enabled := true;
end;

--
Robert

Quote
Amit wrote in message <3c44fb89_2@dnews>...

>HI,
>I have a problem with using threads.

>Problem :
>How can I use VCLs main thread to listen to windows messages posted in
>message queue of a child thread.

>I need to run a main form in which I perform repeated database access.
>Mine is a three tier application hence the data-access may be time
>consuming.
>I want to provide a cancel button, using which the user may cancel current
>activity.

>Basically I will use a GlobalFlag which will be set once the cancel button
>is pressed.
>Each function in my main form, checks for the value of this flag before
>proceeding.

>My problem is that I am not able to trap the cancel button click.
>I tried creating a separate child thread with Execute method looking like
:
>TCancelThread.Execute;
>begin
>  while(True) do
>     Application.ProcessMessages;
>end;
>but it does not trap.
>I also tried to create a separate form for cancel button. I showed it with
>formStyle : fsStayOnTop, and disabled the main form. still it did not trap
>the message.
>How can I trap ONLY Cancel Button Click while my main thread continues it's
>work.

>Rgds
>Amit

Re:Using Threads and Application.processMessage to Cancel operationof main thread.


Robert,
I do not wish to do my main processing in a child thread.
I prefer keeping it in VCL's main thread.
Can I not do this ?
The reason behind this is that my main procedure is not a single method and
it is not even a simple loop.
It is a sequence of functions which are coded in different libraries and are
interdependent. some of them actually wait for the other to finish.
For example, whenever I do database access, I use a SendAndWait.
Which means that I am using a blocking socket. The program execution waits
till sendAndWait finishes. Dependeding on it's result, I change and call
various other functions.
Hence I would prefer keeping it in same main VCL thread. I just want to get
the Application.ProcessMEssages in a different thread so that my app can
somehow process those messages.

Rgds
Amit

Quote
Robert Cerny <robert.qwe.ce...@neosys.xrs.qwe.si> wrote in message

news:a23qoo.4s8.1@neosys.xrs.si...
Quote
> VCL message loop runs in main thread. This means that you have to leave
main
> thread to process user messages and create additional "working" threads:
> procedure TWorkThread.Execute;
> begin
>   while Have_Some_Job and not Canceled do
>     {do your job}
> end;

> procedure Txx.CancelBtnClick();
> begin
>   Canceled := true;
> end;

> procedure Txx.StartBtnClick();
> begin
>   TWorkThread.Create(false);
>   CancelBtn.Enabled := true;
> end;

> --
> Robert

> Amit wrote in message <3c44fb89_2@dnews>...

> >HI,
> >I have a problem with using threads.

> >Problem :
> >How can I use VCLs main thread to listen to windows messages posted in
> >message queue of a child thread.

> >I need to run a main form in which I perform repeated database access.
> >Mine is a three tier application hence the data-access may be time
> >consuming.
> >I want to provide a cancel button, using which the user may cancel
current
> >activity.

> >Basically I will use a GlobalFlag which will be set once the cancel
button
> >is pressed.
> >Each function in my main form, checks for the value of this flag before
> >proceeding.

> >My problem is that I am not able to trap the cancel button click.
> >I tried creating a separate child thread with Execute method looking like
> :
> >TCancelThread.Execute;
> >begin
> >  while(True) do
> >     Application.ProcessMessages;
> >end;
> >but it does not trap.
> >I also tried to create a separate form for cancel button. I showed it
with
> >formStyle : fsStayOnTop, and disabled the main form. still it did not
trap
> >the message.
> >How can I trap ONLY Cancel Button Click while my main thread continues
it's
> >work.

> >Rgds
> >Amit

Other Threads