I use the excellent OmniThreadLibrary library to implement threaded source code parsing, the program need to abandon the existing parsing and restart the parsing whenever the source code is changed.
I do this with the code snippet shown below, is it the correct way? Do I still have to check the Terminated
property of the thread in the ThreadedParseHtml
function?
if FParserThread <> nil then
begin
FParserThread.RemoveMonitor;
FParserThread.Terminate(500);
end;
FParserThread := CreateTask(ThreadedParse);
FParserThread.SetParameter('SourceCode', Editor.Lines.Text);
FParserThread.MonitorWith(FParserThreadMonitor);
FParserThread.Run;
Thanks in advance!
Edit 1: Sorry for reopening this question, but I found memory leaks when FParserThread
is not completed by itself by calling the Terminate
method with enough time given... Any ideas as to what might cause the memory leaks? Thanks!
Edit 2: Read this blog post, I still couldn't figure what the problem might be, since after every steps in ThreadedParse
the code break if Terminated
is ture...
Edit 3: Answering Rob's questions:
In the OnTerminated event handler (not shown here), FParserThread is set to "nil", so by "FParser is completed by itself", I mean the
if FParserThread <> nil then
block is not executed, in that case FParserThread is terminated because it's parsing has been completed.The logic behind the code is that, this is a code editor, upon any code edits there will be a thread being started to parse the source code into the internal tree presentation, in the case when a new code edit happens but the previous parsing thrad hasn't been edited, the program will first forcibly the previous parsing thread then start a new one. This maybe is not a good approach...
Edit 4: After reading this similar SO question, I changed my code to call FParserThread.Terminate
without a parameter which means, if I understand it correctly, that statement will only signal the thread to end, and inside the actual thread task, I applied the logic to exit the thread execution if the Terminated
property is True
.
Now what's wired is that, with the help of Tracetool, I found that after calling FParserThread.Terminate
the OnTaskMessage
event (where I clean up the memories) would not be fired again, that's what caused the memory leaks....