Description
Iocptask is a multi-threaded task delivery and processing unit based on IOCP engine, it is convenient to post the task to IOCP thread for unified scheduling and processing, is to imitate Qdac-qworker processing mode, support D7 above version.
"How to use"
Very simple to use, the following explains a more comprehensive approach:
procedure Postatask (pvtaskwork:tontaskwork; Nil ; = False; = Rtsync); overload;
Parameters:
Pvtaskwork
callback function, procedure (pvtaskrequest:tiocptaskrequest) of object; Pvtaskrequest can access information such as incoming taskdata,strdata.
Pvtaskdata
Is the incoming data, the pointer type, can be any data, in the callback function can be obtained through the Pvtaskrequest object.
Pvruninmainthread:
Whether the callback function runs in the main thread, and takes into account some tasks that require access to the main thread interface, it needs to be run in the main thread.
Pvruntype:
In a synchronous way, two types of support, one is Rtsync is the use of thread synchronization mode, Rtpostmessage, using the message + event waiting mode. The message pattern takes into account the synchronization methods that can be taken when the synchronization method in the DLL is not available.
"Precautions"
When the main form is destroyed, many resources have been destroyed, and if there are tasks that require the main thread to be processed at this time, it may cause the main thread to hang, and the whole process will not end.
destructor Tfrmmain.destroy; begin Flogtask.postatask (onlogmsg, " ABCD " 100 ); Flogtask.active: = false; Flogtask.free; inherited Destroy; end ;
The above code will cause the program to fail to exit,Flogtask.postatask (onlogmsg, 'abcd', True, rtpostmessage); is the task delivered to the main thread, this time the main form is being destroyed, unable to postmessage the corresponding message (Synchronize way is also blocked) cause fmessageevent will continue to wait, It is important to note that the main thread task is not delivered when it is destroyed. You can disable the corresponding posting task in the destroy of the main form, at the beginning of the main form destructor, set enable: = FALSE; This way iocptask no longer handles any tasks, note that the first sentence of the destructor is not the Formdestory event of the form.
destructor Tfrmmain.destroy; begin flogtask.enable:= false; Iocptaskmanager.enable:= False; ..... inherited Destroy; End;
[Diocp3-iocptask manual] Post and callback processing unit for multi-threaded tasks based on the IOCP engine