Saturday, August 22, 2020

Synchronizing Threads and GUI in a Delphi application

Synchronizing Threads and GUI in a Delphi application Multi-stringing in Delphi lets you make applications that incorporate a few concurrent ways of execution. A typical Delphi application is single-strung, which implies all VCL objects get to their properties and execute their strategies inside this single string. To accelerate information preparing in your application, incorporate at least one optional strings. Processor Threads A string is a correspondence channel from an application to a processor. Single-strung projects need correspondence to stream in the two headings (to and from the processor) as it executes; multi-strung applications can open a few distinct channels, hence accelerating execution. Strings GUI At the point when a few strings are running in the application, the inquiry emerges of how you can refresh your graphical UI because of a string execution. The appropriate response lies in the TThread class Synchronize technique. To refresh your applications UI, or principle string, from an optional string, you have to call the Synchronize technique. This strategy is a string safe technique that stays away from multi-stringing clashes that can emerge from getting to question properties or strategies that are not string safe, or utilizing assets not in the primary string of execution. The following is a model demo that utilizes a few catches with progress bars, each progress bar showing the present condition of the string execution. unit MainU;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, ComCtrls, StdCtrls, ExtCtrls;type//interceptor classTButton class(StdCtrls.TButton)OwnedThread: TThread;ProgressBar: TProgressBar;end;TMyThread class(TThread)privateFCounter: Integer;FCountTo: Integer;FProgressBar: TProgressBar;FOwnerButton: TButton;procedure DoProgress;procedure SetCountTo(const Value: Integer) ;system SetProgressBar(const Value: TProgressBar) ;technique SetOwnerButton(const Value: TButton) ;protectedprocedure Execute; override;publicconstructor Create(CreateSuspended: Boolean) ;property CountTo: Integer read FCountTo compose SetCountTo;property ProgressBar: TProgressBar read FProgressBar compose SetProgressBar;property OwnerButton: TButton read FOwnerButton compose SetOwnerButton;end;TMainForm class(TForm)Button1: TButton;ProgressBar1: TProgressBar;Button2: TButton;ProgressBar2: TProgressBar;Button3: TButton;ProgressBar3: TProgressBar;Button4: TButton;Progress Bar4: TProgressBar;Button5: TButton;ProgressBar5: TProgressBar;procedure Button1Click(Sender: TObject) ;end;varMainForm: TMainForm;implementation{$R *.dfm}{ TMyThread }constructor TMyThread.Create(CreateSuspended: Boolean) ;begininherited;FCounter : 0;FCountTo : MAXINT;end;procedure TMyThread.DoProgress;varPctDone: Extended;beginPctDone : (FCounter/FCountTo) ;FProgressBar.Position : Round(FProgressBar.Step * PctDone) ;FOwnerButton.Caption : FormatFloat(0.00 %, PctDone * 100) ;end;procedure TMyThread.Execute;constInterval 1000000;beginFreeOnTerminate : True;FProgressBar.Max : FCountTo div Interval;FProgressBar.Step : FProgressBar.Max;while FCounter FCountTo dobeginif FCounter mod Interval 0 then Synchronize(DoProgress) ;Inc(FCounter) ;end;FOwnerButton.Caption : Start;FOwnerButton.OwnedThread : nil;FProgressBar.Position : FProgressBar.Max;end;procedure TMyThread.SetCountTo(const Value: Integer) ;beginFCountTo : Value;end;procedure TMyThread.SetOwnerButton(const Value: TButton) ;beginF OwnerButton : Value;end;procedure TMyThread.SetProgressBar(const Value: TProgressBar) ;beginFProgressBar : Value;end;procedure TMainForm.Button1Click(Sender: TObject) ;varaButton: TButton;aThread: TMyThread;aProgressBar: TProgressBar;beginaButton : TButton(Sender) ;if not Assigned(aButton.OwnedThread) thenbeginaThread : TMyThread.Create(True) ;aButton.OwnedThread : aThread;aProgressBar : TProgressBar(FindComponent(StringReplace(aButton.Name, Button, ProgressBar, []))) ;aThread.ProgressBar : aProgressBar;aThread.OwnerButton : aButton;aThread.Resume;aButton.Caption : Pause;endelsebeginif aButton.OwnedThread.Suspended thenaButton.OwnedThread.ResumeelseaButton.OwnedThread.Suspend;aButton.Caption : Run;end;end;end. On account of Jens Borrisholt for presenting this code test.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.