How to use a timer for multithreading in Delphi

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.