I use the creation of the method is:
Thread1=class (TThread)
Private
{Private declarations}
Protected
Procedure the Execute; Override.
end;
Then in Thread1. Execute in implementation process, direct use of Thread1. Create create
Then create this way I can get handle to create a thread? If I want to force this thread do??
Question 2: what effect will thread forced termination??
Question 3: terminatethread and the terminate function how to terminate the thread?? If a thread in the while loop, can terminate?
A total of three problem, a 50 points, wish you a great god help me, thank you
CodePudding user response:
My idea is to declare global thread object variable in the main interface, it can use this instance at any time, after starting a thread, if you need to interrupt, is in the thread of the EXECUTE method of judging in the loop body increase, if be interrupted jump out,CodePudding user response:
TypeTTdownLoadThread=class (TThread)
Protected
Procedure the Execute: override;
end;
.
Var
TDownLoad: TTdownLoadThread;
.
Implementation
Procedure TForm1. Button1Click (Sender: TObject);
The begin
TDownLoad:=TTdownLoadThread. Create (false);
.
TDownLoad. Resume;
end;
.
Procedure TTdownLoadThread. Execute;
The begin
FreeOnTerminate:=true;
While XXX do
The begin
If be interrupted then break;//here can't write,
end;
end;
CodePudding user response:
While not Terminated do.
CodePudding user response:
Give an example, for reference only:unit LongWaitTrd;
Interface
USES Classes, Windows, Messages, SyncObjs;
Type TLongWaitTrd=class (TThread)
Private
FMainWin: THandle;
QuitEvent: TEvent;
Procedure SendFeedBackToMainWin ();
Procedure DoTheHardWork ();
Protected
Procedure the Execute; Override.
Public
The constructor Create (CreateSuspended: Boolean); The destructor Destroy; Override.
The function ExitLongWaitTrd () : a Boolean;
Published
The property MainWin: THandle read FMainWin write FMainWin;
end;
Implementation
USES Unit1;
The constructor TLongWaitTrd. Create (CreateSuspended: Boolean);
The begin
Inherited the Create (CreateSuspended);
end;
Destructor TLongWaitTrd. Destroy;
The begin
Inherited;
end;
Procedure TLongWaitTrd. DoTheHardWork ();
The begin
//to do
end;
Procedure TLongWaitTrd. Execute;
Var Msg: TMsg;
The begin
FreeOnTerminate:=True;
//1. The long wait for sample type thread
//while GetMessage (Msg, 0, 0, 0) do
//the begin
//if (Msg) message=WM_USER + 1000) then//tasks to
//the begin
//DoTheHardWork ();
//SendFeedBackToMainWin;
//end;
//if (Msg) message=WM_QUIT) then
//the begin
//QuitEvent. SetEvent;
//Break;
//end;
//end;
//2. Long working type thread sample
//while (true) do
//the begin
//if PeekMessage (Msg, 0, 0, 0, PM_REMOVE) then
//the begin
//if (Msg) message=WM_QUIT) then
//the begin
//QuitEvent. SetEvent;
//Break;
//end;
//end;
//DoTheHardWork ();
//end;
end;
The function TLongWaitTrd. ExitLongWaitTrd;
The begin
Result:=true;
QuitEvent:=TEvent. Create (nil, True, False, 'QuitEvent');
PostThreadMessage (ThreadID, WM_QUIT, 0, 0);
If (QuitEvent WaitFor (2000)=wrTimeOut) then
Result:=false;
QuitEvent. Free;
end;
Procedure TLongWaitTrd. SendFeedBackToMainWin ();
Var Status: String;
The begin
If (MainWin<> 0) then
The begin
Status:='The data from The had been processed by thread.';
PostMessage (MainWin, WM_USER + 2000, 0, an Integer (Status))
end;
end;
End.
CodePudding user response:
The LS, learned a recruit, thankCodePudding user response:
Thank you for the great god, let the thread about flags to exit the way I would have used, just not to use certain special circumstances, such as I receive TCP received message in the thread, use a control to receive is blocking, or behind a lot of time is needed to deal with other functions, then loop back judgment sign a middle time interval is a bit too long, so want to force the thread exit method, and forced the thread out of any defects or bad effect??CodePudding user response:
Examples of the 4th floor, because I have no idea about the way the news, so has not yet been tried, but the way I create a thread is not directly use API function, so can't get a handle, and there are other good solution?? By the way, tell me something about the compulsory end thread will have what not good!!!!!!!!!CodePudding user response:
Windows on the thread object Handle to Handle isCodePudding user response:
SOCK obstruction, to ask, I know JAVA, the same problem, can't interrupt thread group game, later changed to Netty, Delphi similar frame?CodePudding user response:
Baidu, also seems to have a non-blocking modeCodePudding user response:
Is commonly used while judgment Terminated in propertiesCodePudding user response:
In the main thread unit set a bStop: Boolean; Initialized to False; The thread loop can visit bStop, if bStop then break; Jump out of the loop, can Execute over,,,,,,, generally end in this way the thread is ok, end of the "natural", of course you also can end violence, set a variable in the main thread th1: Thread1, this variable in the build thread thread is used to record the data, need to the end of the violence, with th1 directly. The TerminateCodePudding user response:
X, all say blocked also flags, it will stop until there are data in thereCodePudding user response:
I am used to so get
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull