How to display the current state of the sub-thread

xiaoxiao2021-03-06  55

How to display the current state of the sub-thread

A small program just completed is to analyze a data file using a vertical arrangement of 24-pin printhead, analyze some parameters such as the number of times of each needle, and life analysis to the mechanical development department.

Since the data file may be large, the analysis time will be longer, so it is possible to display the analysis schedule, otherwise it will make the program to die.

Therefore, the analysis work is placed in the sub-thread, and the main thread update state is notified by generating "state change event" and "analysis end event". The thread is defined as follows:

/ / ==================================

// mythread.pas

/ / =================================== //

TSTATUSCHANGEDEVENT = Procedure (Percent: integer) of object; // Status change event

TfinishedEvent = procedure of object; // Analysis end event

Tmythread = Class (TTHREAD)

Private

Fonstatuschanged: TSTATUSCHANGEDEVENT;

Fonfinished: tfinished: tfinished;

protected

Procedure execute; override;

public

Property OnStatuschanged: TSTATUSCHANGEDEVENT Read FonstatusChanged Write FonstatusChanged;

Property onfinished: Tfinished Write Fonfinished;

END;

Procedure Tmythread.execute;

VAR

PERCENT: INTEGER;

Begin

...

IF (Percent has changed) And Assigned (Fonstatuschanged) THEN

FonstatusChanged (Percent);

...

// Analysis end

IF Assigned (Fonfinished) THEN

Fonfinished; // Notify the main thread at the end of the analysis

END;

The status display is implemented by a form, which has a progress bar, a state Label, a cancel button, and a timer. Define as follows (no progress bar, Label, button code)

// =====================================

// tfrmstatus.pas

/ / ============================================= // TFRMSTATUS = Class (TFORM) // Display Progress form

Timer1: TTIMER;

Procedure formcreate (Sender: TOBJECT);

Private

Procedure setpercent (Value: integer);

Procedure setterminate (Value: Boolean);

Public

Property Percent: Integer Read Fpercent Write SetPerCent;

Property Terminate: Boolean Read Fterminate Write Setterminate;

END;

Procedure TfrmStatus. Formcreate (Sender: TOBJECT);

Begin

Fterminate: = false;

Timer1.enable: = FALSE;

END;

Procedure TfrmStatus. setpercent (value: integer); // Setting percentage progress

Begin

/ / Set the control status to indicate the current percentage progress

END;

Procedure tfrmStatus.setterminate (Value: Boolean;) // Set the end status

Begin

IF fterminate <> value kil

Begin

Fterminate: = value;

Timer1.interval: = 2;

Timer1.enable: = TRUE;

END;

END;

Procedure TfrmStatus.timer1Timer (Sender: TOBJECT); / / Response Timer, Close Window, Return to MROK

Begin

CLOSE;

ModalResult: = mrok;

END;

// >>>>>>>>>>>>>>>>>>>>>>>>>>>

The main form is responsible for creating an analysis thread and status display form, and responding to "Status change event" generated by the analysis thread and "Analysis Events".

// main form

TfrmMain: = Class (TFORM)

Private

FFRMSTATUS: TFRMSTATUS;

Procedure DostatusChange (Percent: Integer); / / Response Analysis Progress Change Event

Procedure dofinished; // Response analysis end event

public

PROCEDURE DO;

END;

// operation analysis

Procedure tfrmmain.do;

VAR

Mythread: Tmythread;

FRMSTATUS: TFRMSTATUS;

Begin

Try

Mythread: = Tmythread.create (TRUE);

Mythread.onstatuschange: = dostatuschange;

Mythread.onfinished: = DOFINISHED;

Try

FRMSTATUS: = TfrmStatus.create (nil);

FfrmStatus: = frmstatus; mythread.resume; // Start thread

IF frMstatus.showmodal = mrok dam

Showanalyseresult; // Display analysis results

Finally

FFRMSTATUS: = NIL;

FRMSTATUS.FREE;

END;

Finally

Mythread.freeonterminate: = true;

Mythread.terminate;

END;

END;

/ / Response status change event, display progress

Procedure tfrmmain.dostatuschange (percent: integer)

Begin

IF FFRMSTATUS <> NIL THEN

FFRMSTATUS.PERCENT: = percent;

END;

转载请注明原文地址:https://www.9cbs.com/read-82259.html

New Post(0)