hidrau
asked on
Working with a simple thread
Hello Guys, I need to create a simple thread where I can insert this code below in my unit, but Thread is something new for me and I need you help. Let me explain what I need.
I have a button that calls a storeprocedure, it takes approximately between 5 and 10 minutes, it depends on the month, the user needs to know that the process is on and needs to wait until it finishes. For this I want to create a progress bar in my form that is the same where I call the storeprocedure through ADOStoredProc. While it is working I want to start a loop where a progressbar is shown, like this code:
procedure startLoop;
Const
V = 100;
Var
I : Integer;
begin
Para := False;
ProgressBar1.Max := V;
I := 0;
while Not Stop do
Begin
If I < V Then For I := 0 to V do
Begin
Application.ProcessMessage s;
ProgressBar1.Position := I;
Sleep(01);
End;
If I > V Then For I := V downto 0 do
Begin
Application.ProcessMessage s;
ProgressBar1.Position := I;
sleep(01)
End;
End
end;
You can note here that there is a variable named Stop, it is a boolean variable and when my storeprocedure finishes I will set it to true.
Alex
I have a button that calls a storeprocedure, it takes approximately between 5 and 10 minutes, it depends on the month, the user needs to know that the process is on and needs to wait until it finishes. For this I want to create a progress bar in my form that is the same where I call the storeprocedure through ADOStoredProc. While it is working I want to start a loop where a progressbar is shown, like this code:
procedure startLoop;
Const
V = 100;
Var
I : Integer;
begin
Para := False;
ProgressBar1.Max := V;
I := 0;
while Not Stop do
Begin
If I < V Then For I := 0 to V do
Begin
Application.ProcessMessage
ProgressBar1.Position := I;
Sleep(01);
End;
If I > V Then For I := V downto 0 do
Begin
Application.ProcessMessage
ProgressBar1.Position := I;
sleep(01)
End;
End
end;
You can note here that there is a variable named Stop, it is a boolean variable and when my storeprocedure finishes I will set it to true.
Alex
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I don't think Synchronize is necessary here as the main thread does not access any of the VCL objects while the progress thread is running. The only call between the progress thread start and end is the execution of a stored procedure which is at database level i.e. nothing else can happen in the app while the thread is running until such time as an error occurs or the SP is done both instances which are taken care of by try blocks.
Normally I would agree that it is good practise, however, in this case I didn't see the need. Also I thought it could possibly prevent the intended execution (correct me if I'm wrong). I'll explain what I mean:
- synchronize will run the called procedure in the main thread
- after the SP.Execute, the main thread is waiting for the call to finish
- could this not result in the progressbar standing still? (would it not wait for the main thread to finish the SP.Execute call before running?)
Regards
Pierre
ASKER
thanks
Nice solution... but all the stuff in your execute method should rather look like this:
Synchronize(Setup);
While not(Terminated) or not(Stop) do Begin
Synchronize(UpdateProgress
End;
You are working with VCL components and if you don't use Synchronize, your application can get EAccessViolation exceptions.
Mainiacfreakus