Home > Back-end >  Delphi multithreaded with parameters
Delphi multithreaded with parameters

Time:09-25

How to create a parameter of multithreading, trouble will TForm1. Threadtest change into one with parameters of multithreading, use tthread class

 
The unit Unit1;

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls ComCtrls;

Type
TForm1=class (TForm)
For: TButton;
Button2: TButton;
ProgressBar1: TProgressBar;
Label1: TLabel;
Procedure Button1Click (Sender: TObject);
Procedure threadtest strtest: (string);
Procedure Button2Click (Sender: TObject);
Procedure FormCreate (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;
Var
Form1: TForm1;

Implementation

{$R *. DFM}
Procedure TForm1. Threadtest (strtest: string);
The begin
Showmessage (strtest);
end;
Procedure TForm1. Button1Click (Sender: TObject);
The begin
Threadtest (" STR ");
end;

Procedure TForm1. Button2Click (Sender: TObject);
Var
I: Integer;
The begin
For I=0 to ProgressBar1. Max do
The begin
Label1. Caption:='in the operation of the main thread;
ProgressBar1. Position:=I;
Application. ProcessMessages;
end;
end;

Procedure TForm1. FormCreate (Sender: TObject);
The begin
Progressbar1. Max:=1000000;
end;

End.

CodePudding user response:


Great god genuflect is begged for help, this problem is very entanglements,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

CodePudding user response:

Beginners Delphi, true don't know how to modify,,,

CodePudding user response:

upupupupup

CodePudding user response:

This is a question worth thinking about,,,,

CodePudding user response:

How does not seem to understand

CodePudding user response:

The first way is to join the thread class destructor, limitations, suitable to create a thread not hung thread immediately get your parameters passed to the normal operation, the second is the thread class publish domain to add the property, use, create a thread object, the parameters into the into the property, its tip: don't make the hanging thread to thread synchronization, synchronization technology should be used to synchronize threads,

CodePudding user response:

refer to 6th floor a295281315 response:
the first way is to join the thread class destructor, limitations, suitable to create a thread not hung thread immediately get your parameters passed to the normal operation, the second is the thread class publish domain to add the property, use, create a thread object, the parameters into the into the property, its tip: don't synchronize the threads to hang up a thread, you should use synchronization technology to synchronize threads,

Specific how to operate it, just learning Delphi, do not know how to deal with?

CodePudding user response:

TMyThread=class (TThread)
FParam: string;

The constructor Create (AParam: string);
end;

The constructor TMyThread. Create (AParam: string);
The begin
FParam:=AParam;
Inherited the Create (True);
end;

CodePudding user response:

TThread class, and how many people did, made from CreateThread, simple, practical, and the original API, do not add any preservative,
API:
CreateThread
The TerminateThread
The WaitForSingleObject
InitializeCriticalSection
DeleteCriticalSection
The EnterCriticalSection
LeaveCriticalSection

CodePudding user response:

Your problem, remind:

One, do not use thread API, direct use of Delphi s thread class Tthread is very convenient and it has closed the necessary methods,

Two, in the thread, do not use a For loop, it inconvenience to suspend, recommend the use of Repeat loop, set a global variable ThreadEnd, dedicated to suspend threads,

Three, in the procedure TThread1. Execute, is a thread that executes the code, as long as code execution is completed, the thread automatically terminated, release the memory,

Here is to modify the source code of your code, only supplies the reference:
 unit Unit1; 

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls ComCtrls;

Type
TThread1=class (Tthread)
Protected
Procedure the Execute; Override.
end;

Type
TForm1=class (TForm)
For: TButton;
ProgressBar1: TProgressBar;
Button2: TButton;
Procedure FormCreate (Sender: TObject);
Procedure Button1Click (Sender: TObject);
Procedure Button2Click (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;

Var
Form1: TForm1;
ThreadEnd: Boolean;

Implementation

{$R *. DFM}

Procedure TThread1. Execute;//thread execution
Var
I: Integer;
The begin
I:=0;
ThreadEnd:=False;
FreeOnTerminate:=true;
Repeat
I:=I + 1;
Form1. ProgressBar1. Position:=I;
Application. ProcessMessages;
Until (I>=Form1. ProgressBar1. Max) or ThreadEnd;
end;

Procedure TForm1. FormCreate (Sender: TObject);
The begin
Progressbar1. Max:=100000;
end;

Procedure TForm1. Button1Click (Sender: TObject);
Var
MyThread: TThread1;
The begin
MyThread:=TThread1. Create (False);//create a thread and thread of execution
end;

Procedure TForm1. Button2Click (Sender: TObject);
The begin
ThreadEnd:=True;//to suspend the thread
end;

End.




CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related