Carried out with the problem now is the first time, I don't know how to call the second time, when I create is to create (true), the first resume normal execution threads, onterminate events also normal execution, but the second I don't know how to perform, or thread can only be a one-off, need new to create again the second time?
There is a problem OnTerminate thread end, execution is in what state, I TForm1. Button2Click (Sender: TObject); Terminated is False, the time in the calling thread according to intuitive understanding, it should be True, warrior, please help solve,
{TMyThread}
The constructor TMyThread. Create (ASuspended: Boolean);
The begin
Inherited the Create (ASuspended);
end;
Procedure TMyThread. Execute;
The begin
Inherited;
ShowMessage (' 3 ');
end;
{TForm1}
Procedure TForm1. FormDestroy (Sender: TObject);
The begin
If Assigned (FMyThread) then FMyThread. Destroy;
end;
Procedure TForm1. Button1Click (Sender: TObject);
The begin
If not Assigned (FMyThread) then
The begin
FMyThread: TMyThread.=the Create (True);
FMyThread. OnTerminate:=ThreadTerminate;
FMyThread. Resume
End
The else FMyThread. Resume
end;
Procedure TForm1. ThreadTerminate (Sender: TObject);
The begin
ShowMessage (' jieshule ');
end;
Procedure TForm1. Button2Click (Sender: TObject);
The begin
If FMyThread. Terminated then ShowMessage (' Terminated ')
The else ShowMessage (' not Terminated ');
end;
CodePudding user response:
Procedure TMyThread. Execute;The begin
Repeat
//do something
Suspend;
Until Terminated;
end;
CodePudding user response:
I was achieved through the method of event + WaitForObject, after 2010 or 2007, suspend method has been abandoned, the realization of can under reference Ihttp://blog.csdn.net/jankercsdn/article/details/8874469
About thread exit process, there are still not perfect, there are changes in the actual use, after finishing the hair again,
CodePudding user response:
Changes: http://blog.csdn.net/jankercsdn/article/details/51323690CodePudding user response:
Typical usage, make full use of the Delphi closed TthreadA, declare a own reference Tthread class
Type
TGaugeThread=class (Tthread)
Protected
Procedure the Execute; Override.
end;
Two, declare a thread global variables, a Boolean variable, used for their control to close the thread
Var
MyThread: TGaugeThread;
ThreadOn: Boolean;
Part three, write an implementation of the
Procedure TGaugeThread. Execute;
Var I: integer;
The begin
ThreadOn:=True;
ThreadEnd:=False;
FreeOnTerminate:=true;
I:=0;
Repeat
I:=I + 1;
If i>=99 then I:=1;
GaugeFrm. Gauge1. Progress:=I;
Application. ProcessMessages;
Sleep (75);
Until Terminated or ThreadEnd;
GaugeFrm. Close;
ThreadOn:=False;
end;
Four, the calling thread (can be called multiple times)
MyThread:=TGaugeThread. Create (False);
Five, shut off the thread
ThreadEnd:=False
CodePudding user response:
On Windows is no problem, because the Suspend is actually the API SuspendThread packaging, but may not be implemented on android, mark deprecated is not abandoned, can't use, but don't agree with use, obsolete meaning (for the sake of cross-platform),CodePudding user response:
Brothers, you have no according to answer my question, you said that the content of the said I knowCodePudding user response:
You describe the problem there are ready-made, "thread pool", thread needs to keep out from a List of tasks to perform, no task is blocked in quests, multithreading, of course, want to consider the problem of resource competition, you can search for the "Delphi thread pool implementation"CodePudding user response: