AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

mutiple procedures in threads

Ein Thema von question · begonnen am 31. Dez 2013 · letzter Beitrag vom 31. Dez 2013
Antwort Antwort
question

Registriert seit: 17. Apr 2013
97 Beiträge
 
#1

mutiple procedures in threads

  Alt 31. Dez 2013, 09:51
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?
  Mit Zitat antworten Zitat
Benutzerbild von Bernhard Geyer
Bernhard Geyer

Registriert seit: 13. Aug 2002
17.196 Beiträge
 
Delphi 10.4 Sydney
 
#2

AW: mutiple procedures in threads

  Alt 31. Dez 2013, 11:09
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, ...)
Windows Vista - Eine neue Erfahrung in Fehlern.
  Mit Zitat antworten Zitat
Furtbichler
(Gast)

n/a Beiträge
 
#3

AW: mutiple procedures in threads

  Alt 31. Dez 2013, 13:32
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.
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.
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:15 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz