Sunday, November 24, 2019

Synchronizing Threads and GUI in a Delphi application

Synchronizing Threads and GUI in a Delphi application Multi-threading in Delphi lets you create applications that include several simultaneous paths of execution. A normal Delphi application is single-threaded, which means all VCL objects access their properties and execute their methods within this single thread. To speed up data processing in your application, include one or more secondary threads. Processor Threads A thread is a communication channel from an application to a processor. Single-threaded programs need communication to flow in both directions (to and from the processor) as it executes; multi-threaded apps can open several different channels, thus speeding up execution. Threads GUI When several threads are running in the application, the question arises of how you can update your graphical user interface as a result of a thread execution. The answer lies in the TThread class Synchronize method. To update your applications user interface, or main thread, from a secondary thread, you need to call the Synchronize method. This technique is a thread-safe method that avoids multi-threading conflicts that can arise from accessing object properties or methods that are not thread-safe, or using resources not in the main thread of execution. Below is an example demo that uses several buttons with progress bars, each progress bar displaying the current state of the thread 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) ;procedure SetProgressBar(const Value: TProgressBar) ;procedure SetOwnerButton(const Value: TButton) ;protectedprocedure Execute; override;publicconstructor Create(CreateSuspended: Boolean) ;property CountTo: Integer read FCountTo write SetCountTo;property ProgressBar: TProgressBar read FProgressBar write SetProgressBar;property OwnerButton: TButton read FOwnerButton write 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) ;b eginFOwnerButton : 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. Thanks to Jens Borrisholt for submitting this code sample.

Thursday, November 21, 2019

In the communist manifesto karl marx states capital is... not a Essay

In the communist manifesto karl marx states capital is... not a personal, but a social power. Discuss what marx means by this - Essay Example In the book â€Å"The Communist Manifesto†, Karl Marx has shown the victory of the disciples of proletarian in the communist society. In the ancient period, all the powers of the old Europe were attracted towards the holy union in order to exercise the communism. The holy union was among the spies of German police and French radicals, Pope and Tsar and the Guizot and Metternich. During this period, it was highly realized by the Europeans that the communists must openly face the entire world and meet the study of communism with the policy of the party itself. Karl Marx had also stated that the capital is not a personal but a social power. A lucid explanation of this statement would be further discussed in the essay on the basis of the bourgeois, proletarians and the communists. Discussion on the Statement i.e. "Capital is... Not a Personal, But a Social Power" and Marx’s Use of the Word â€Å"Parasite† It has been a trend that the simple words ‘oppressorâ₠¬â„¢ and ‘oppressed’ have marked several histories of open fights, which ended with a revolution among the different classes of people in the society or destruction of a particular class of people. Likewise, the revolutionary action among the bourgeois and proletarians resulted in the abolition of the capitalists’ society and introduced communism in the society. Bourgeois and proletarians are the two major dimensions of the communists’ society in the ancient times. Bourgeois were the modern capitalists acting as the owner of the production activities and the employer of the labors. On the other hand, proletarians were the group of laborers who did not have their own modes of production and were compelled to sell their labor power to the bourgeois. At that period of time, bourgeois were holding the supreme power in the society as they were the middle class owners of the production activities and the labors. The labors were the proletarians who had no power in the society rather they were treated as the private labor of the bourgeois. The proletarians were compelled to sell their labors to the bourgeois in order to earn their living. The proletarians were suppressed by the bourgeois as they had no rights of property and self-esteem in that society. Nonetheless, it was frequently observed that the communists had represented the interests of the proletarians as a whole in the movement during their fight for the property and rights1. The communists had never opposed the other working class entities of the society. They always formed similar party to that of the working class. They served the similar aims of the party as the proletariat does. The distinction between the communists and the other working parties arrive on the basis of two of the grounds. The first base is that the communists had brought the common interest of the proletarians in light during their national fight in various countries. The other is that the communists were sig nifying the interests of all the parties in the movement as a whole during the stages of development of the proletarian society. These two characteristics of the communists not only made them different from the other working class parties in the world but they had also made them the most advanced party in the entire world. The communists had the similar aim to that of the proletarians. The aims of both these parties were end of the bourgeois superiority, development of proletariat into a class and invasion of the political