In Delphi, A tthread multi-thread class is provided. developers can customize the multi-thread function based on their own needs, and the timer has a wide range of applications in multithreading. Here, let's just talk about some key issues about waitforsingleobject as a timer. (For more information about timer, see "Deep Adventure".)
Waitforsingleobject is an API function. To use this function, you must call createevent and timesetevent before using it. However, the positions declared in the event thread here are different and the effects are different. The following two declaration methods are provided:
Unit ptunit;
Interface
Uses
Classes, mmsystem, windows, sysutils, forms;
Type
Tpingthread = Class (tthread)
Private
{Private Declarations}
{Declare event as a private data member of this thread class}
Timerid: integer;
Htimerevent: thandle;
Ffilename: string;
Protected
Procedure execute; override;
Public
Constructor create;
Procedure setover;
Published
End;
Implementation
Constructor tpingthread. Create;
Begin
Freeonterminate: = true;
Inherited create (true );
End;
Procedure tpingthread. setover;
Begin
Timerid: = timesetevent (5, 0, tfntimecallback (htimerevent), 0, time_periodic or time_callback_event_set );
End;
Procedure tpingthread. Execute;
Begin
Htimerevent: = createevent (nil, false, false, nil );
Timerid: = timesetevent (5*1000,0, tfntimecallback (htimerevent), 0, time_periodic or time_callback_event_set );
Repeat
If waitforsingleobject (htimerevent, infinite) = wait_object_0 then
Begin
If terminated then break;
Dosomething;
End;
Until false;
Timekillevent (timerid );
Closehandle (htimerevent );
End;
End.
This declaration ensures that the thread can be executed normally. However, the following declaration method does not work. When you only create a thread instance, it can still be executed normally. If you create more than two thread instances, it is not correct.
Unit ptunit;
Interface
Uses
Classes, mmsystem, windows, sysutils, forms;
Type
Tpingthread = Class (tthread)
Private
{Private Declarations}
Ffilename: string;
Protected
Procedure execute; override;
Public
Constructor create;
Procedure setover;
Published
End;
Implementation
{Declaring events in Implementation}
VaR
Timerid: integer;
Htimerevent: thandle;
Constructor tpingthread. Create;
Begin
Freeonterminate: = true;
Inherited create (true );
End;
Procedure tpingthread. setover;
Begin
Timerid: = timesetevent (5, 0, tfntimecallback (htimerevent), 0, time_periodic or time_callback_event_set );
End;
Procedure tpingthread. Execute;
Begin
Htimerevent: = createevent (nil, false, false, nil );
Timerid: = timesetevent (5*1000,0, tfntimecallback (htimerevent), 0, time_periodic or time_callback_event_set );
Repeat
If waitforsingleobject (htimerevent, infinite) = wait_object_0 then
Begin
If terminated then break;
Dosomething;
End;
Until false;
Timekillevent (timerid );
Closehandle (htimerevent );
End;
End.
The above two functions are the same except that the event declaration locations are different.CodeWhen more than two thread instances are created, the first thread can be normally executed, and the timer interval in the second thread will fail. When one of the threads ends, another thread cannot work normally.
Conclusion: We recommend that you use the first statement to ensure normal operation.