Home > Back-end >  Program exits thread execution of the how to solve
Program exits thread execution of the how to solve

Time:09-21

I create a class to manage a single thread, the thread is the structure of the


While not terminated do
The begin

While (iThe begin

Here is a long mission, that's the problem, the user if close the program, there has not been performed? Here is written to the database
Data operation, is very time consuming, if a user exit, I must ensure that the data is written to the database, want to know how to achieve the user exit button when
Must wait until the task is finished, the program really quit
end;
end;

CodePudding user response:

1, using the TEVENT variables, the child thread starts running when no signal, at the end (the child thread EXCUTE last) signal, the main program exit when waiting for the signal,
AEvent. WaitFor (AWaitTime)
2, threads run variable is no signal, when the end of the signal, can use this, use the WaitForSingleObject () or WaitForMultipleObjects ()

Reference: http://blog.csdn.net/jankercsdn/article/details/51323690

CodePudding user response:

If you are using XE and later, you can use TCountdownEvent
 program Project1. 

{$APPTYPE CONSOLE}

USES the
SysUtils,
Classes,
SyncObjs;

Type
MyThread=class (TThread)
Protected
Procedure the Execute (); Override.
end;

Var
Wait: TCountdownEvent;

{MyThread}

Procedure MyThread. Execute;
The begin
Sleep (Random (3000));
Wait. The Signal ();
end;

Var
I: Integer;
Const
THREAD_COUNT=100;
The begin
Wait:=TCountdownEvent. Create (THREAD_COUNT);
For I:=1 to THREAD_COUNT do
The begin
MyThread. Create (False);
end;
Wait. WaitFor ();
End.

  • Related