Home > Back-end >  Variable survival problems
Variable survival problems

Time:09-20

I have a form, one of the global variable myThread01

Type
Tmainform=class (TForm)
.
end;

Var
Mainform: Tmainform;
MyThread01: TThread;

A function call in Tmainform myThread01


Procedure Tmainform. Thread3;
The begin
//loop forever thread 1
MyThread03:=TThread CreateAnonymousThread (
Procedure
The begin
While true do
The begin
.
end;
End);
MyThread03. Priority:=tpTimeCritical;
MyThread03. Start;
end;

Is an infinite loop myThread01 code,

How much is this myThread01 lifetime? I found that sometimes, after open the child form myThread01 failure would happen, don't know if it is related to survival??

CodePudding user response:

Your code does not make thread standard (problem)
1, define an own thread type
Type
TMyThread=class (Tthread)
Protected
Procedure the Execute; Override.
end;
2, to write a thread entity process
Procedure TMyThread. Execute;
The begin
ThreadOn:=True;
ThreadEnd:=False;
FreeOnTerminate:=true;
I:=0;
Repeat
,,,,,,,
Sleep (75);
Until Terminated or ThreadEnd;
ThreadOn:=False;
end;
3, declare a corresponding variable
Var MyThread: TMyThread;
It is global, it points to the address of the thread,

4, to create the calling threadMyThread: TMyThread.=the Create (); Create a thread, it will implement body:

So as to realize the thread, the question: as for the thread into uniform for a long time, to see whether the MyThread. Execute; Code execution, execution of the thread is automatically released,

CodePudding user response:

Typing error:
So as to realize the thread, the question: as to how long threads Execute survival, depends on MyThread. Execute; Code execution, execution of the thread is automatically released,

As for survival
Variable MyThread it is global, thread, depends on the existence of the realization of the thread part code MyThread. Execute; Execution time,

CodePudding user response:

Procedure Tmainform. Thread3;
No see access to myThread01? MyThread03 dialed the wrong number?

CodePudding user response:

What's the relationship between myThread01 and myThread03. Write wrong? Exit the thread after release need to set up FreeOnTerminate:=True; Otherwise need to explicitly release, there is no exit condition cycle is death cycle, does not automatically quit,

CodePudding user response:

reference 1st floor lyhoo163 response:
your code does not make thread standard (problem)
1, define an own thread type
Type
TMyThread=class (Tthread)
Protected
Procedure the Execute; Override.
end;
2, to write a thread entity process
Procedure TMyThread. Execute;
The begin
ThreadOn:=True;
ThreadEnd:=False;
FreeOnTerminate:=true;
I:=0;
Repeat
,,,,,,,
Sleep (75);
Until Terminated or ThreadEnd;
ThreadOn:=False;
end;
3, declare a corresponding variable
Var MyThread: TMyThread;
It is global, it points to the address of the thread,

4, to create the calling threadMyThread: TMyThread.=the Create (); Create a thread, it will implement body:

So as to realize the thread, the question: as for the thread into uniform for a long time, to see whether the MyThread. Execute; Code execution, execution of the thread is automatically released,
CreateAnonymousThread thread is a new version of usage, have no wrong,

CodePudding user response:

General purpose use CreateAnonymousThread create anonymous thread is outside don't visit!
Access and thread this stuff, generally do not do, I also didn't see mythread01, thread the life cycle of general thread function finished the execute, thread running cycle is done, and if the FreeOnTerimte this set to true, will be automatically released and object does not exist, or thread object is still there, but the thread is over
  • Related