Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   mutiple procedures in threads (https://www.delphipraxis.net/178296-mutiple-procedures-threads.html)

question 31. Dez 2013 08:51

mutiple procedures in threads
 
Dear all,

As there is one topics has been already discussed about multi-threading, neverthless i would like to ask one question.

In Delphi , i have created several procedures to save data into DB, and i have call all the procedure in button-click event, for example
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
saveTopersonal(); // i would like to use multi-thread for all the procedures with a status-bar
savechild();
saveroot();
saveToAdmin();
saveTopublic();
saveSecretData
end;
i would like to use multi-threading with a status bar, when one procedure will execute then this status-bar will increase and show a message-which procedure is saving currently. could anybody please help me, how can i use multi-thread ,in my situation and with a status-bar?

Bernhard Geyer 31. Dez 2013 10:09

AW: mutiple procedures in threads
 
You will need to create a database connection for every thread.

But I think that this isn't a good idea because all of the data normale should be saved in a transaction to avoid data inconsistence.
If you have problems in performance we should taka a look at you code to save the data. Perhaps you have some issue that are caused by unfavorable implementation (not using of prepared statements, ...)

Furtbichler 31. Dez 2013 12:32

AW: mutiple procedures in threads
 
You are free to process all your SaveXXXX() procedures in *one* separate thread. multiple threads is useless as the individual methods have to be processed in order I guess.
Zitat:

Zitat von question (Beitrag 1241540)
I would like to use multi-thread for all the procedures with a status-bar

This is a contradiction: If you want to process all methods simultaneously, there is no need for a status bar.

However, How about this (untested, might be close to Delphi):
Delphi-Quellcode:
Type
TNotifyStringEvent = Procedure (Sender : TObject; msg : string) of Object;
TSaveThread = Class (TThread)
private
  fMsg : String;
  fOnShowStatus : TNotifyStringEvent;
Protected
  Procedure Execute: Override;
public
  Constructor Create; Override;
  OnShowStatus : TNotifyStringEvent Read fOnShowStatus write fOnShowStatus;
End;

Constructor TTSaveThread.Create;
Begin
  Inherited Create(True);
  FreeOnTerminate := true;
End;

Procedure TTsaveThread.ShowStatus (msg : String);
Begin
  fMsg := msg;
  Synchronize(DoShowStatus);
End;

Procedure TSaveThread.DoShowStatus;
Begin
  If Assigned (fOnShowStatus) then fOnShowStatus(Sender, fMsg);
End;

Procedure TTSaveThread.Execute;
Begin
  ShowStatus('Saving Child');
  savechild();
  ShowStatus('Saving Root');
  saveroot();
  ShowStatus('Saving Admin');
  saveToAdmin();
  ShowStatus('Saving To Public');
  saveTopublic();
  ShowStatus('Saving Secret Data');
  saveSecretData();
end;

...
Procedure TMyForm.ButtonClick(Sender : TObject);
Begin
  ProgressBar.Minimum := 1;
  ProgressBar.Maximum := 5;
  ProgressBar.Position := 1;
  myThread := TSaveThread.Create;
  myThread.OnShowStatus := MyShowStatus;
  myThread.OnTerminate := SaveDone;
  myThread.Resume;
End;

Procedure TMyForm.SaveDone (Sender : TObject);
Begin
  Showmessage('Save finished');
End;

Procedure TMyForm.MyShowStatus (Sender : TObject; msg : String);
Begin
  lbProgress.Caption := msg;
  ProgressBar.Step;
End;
This will create a thread which will do the work in the background leaving the form operational. When a step is finished, it calls an event and the subscriber (our form) can update the progress bar and a label.

However, you must include logic to cancel the thread in case your form is closed. Otherwise the event call will access a nonexistent or at least invisible form. Also, you might think that closing the form should be either forbidden, wait for the thread to finish or cancel the thread's execution.

My advice: Study the thread demos thoroughly.

PS: There is a much simpler approach without a thread. just update the progressbar and the label prior to executing each step in the main form. However this puts your form in a blocking state and you have to take care on processing messages in between yourself. I would prefer using a thread and add logic to let the form/thread behave properly on any attempt to close the form.


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:42 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz