I am writing a multi-threaded application. There is a form. When a user clicks a button on a form, a stream is created in which files on all local drives are searched.

Is it necessary to manually " Terminate and " Free flow during the destroy form? For example, if the user closes the program window or terminates the process while executing the stream. Or is it not necessary and when destroying the form, the resources of the stream will be released "by themselves"?

    1 answer 1

    Of course, you need to free up resources. If FreeOnTerminate: = True, then it is enough to do

     FThread.Terminate FThread.WaitFor; 

    If FreeOnTerminate: = False, then you also need to do Free:

     FThread.Terminate FThread.WaitFor; FThread.Free; 

    But in order for Terminate to work it is necessary to monitor the terminated variable in the Execute procedure:

     procedure TTestThread.Execute; begin while not Terminated do begin ... end; end; 
    • 2
      If FreeOnTerminate := True , then it’s not so easy to do, because by the time it is accessed, the stream may already be completed and free - we get access to the freed object with appropriate effects. - Kromster