Home > Back-end >  Thread Thread enable problem, very simple, online, etc., knot posted immediately. Ready to points!
Thread Thread enable problem, very simple, online, etc., knot posted immediately. Ready to points!

Time:12-08

Idea is that the threads created according to the need to perform, the scene after the execution, not logout, next time will continue to enable implementation,
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 I
http://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/51323690

CodePudding user response:

Typical usage, make full use of the Delphi closed Tthread

A, 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 know

CodePudding 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:

refer to 6th floor eFainter response:
brothers, you have no according to answer my question, you say that I know the content of the

Actually Delphi to the design of TThread is to Execute process using Windows multi-threading mechanism run once, so when the Execute process execution, multi-threaded ended,
Say so, you want to make thread alive all allow you to use at any time, you can't let the Execute over, then you will need to write a loop in the Execute (while/repeat), free time Sleep to prevent too much CPU, a task, set a variable, the circulation after inspection to this variable in the Execute continues to perform the task (the task can be you write good function or other processes).

CodePudding user response:

Thread is performed automatically end, or CPU, will cause the system to block,

CodePudding user response:

Agree with upstairs,
Or add a timer, calls constantly

CodePudding user response:

Procedure TMyThread. Execute;
The begin
While not terminated do
The begin
DoSomeThing;
FEvent. WaitFor (INFINITE);
end;
end;

If you want the thread to run again, take FEvent to signal, FEvent. SetEvent;

CodePudding user response:

Use the semaphore control!

CodePudding user response:

1. Look at the situation, if the thread is not very frequent start-stop, then use the time to create, when not destroyed,
Here is my written communication of the com start-stop,

Procedure TAlmDev. Start;
The begin
Try
FComPort. Open;
FStop:=False;
Except on e: the exception do
MsgBox (Format (' % s: % s', [FComPort Port, e.m essage]));
end;
//
If FComPort. Connected and (FComThread=nil) then
The begin
FComThread:=TCommThread. Create (Self);
end;
end;

Procedure TAlmDev. Stop;
The begin
FStop:=True;
FComPort. Stop;
//
If FComThread & lt;> Nil then
The begin
FComThread. Free;
FComThread:=nil;
end;
//
FComPort. Close;
end;

2, if it is a very frequent start-stop, similar to the thread used in the buffer pool, can use the event, the thread when not in use in the blocking state, used when the time comes to send signals to the event, thread starts circulating,

3, there is a kind of usage is more special, is in the external threads, call suspend and resume to start stop threads, but this method is not commonly used, mainly is the time to start stop threads running status is not very good,

For the original poster said ontermitate triggered events that thread has been destroyed, can't be in the resume,