Forum > WinCE
Thread in scanner will not terminated
(1/1)
mangakissa:
I've created an application for Symbol MC9190 with uscanner.pas. This object (declared as scanner := TStanner) runs in another thread. When I start a form, TScanner object will be started. After closing the form I use this procedure:
--- Code: --- {$IFDEF LCLWinCE}
if scanner <> nil then
scanner.Terminate;
{$ENDIF LCLWinCE}
--- End code ---
But I still can use the scanner on the terminal. When I open the form again, the application comes with this message:
--- Quote ---Error Scanning
Code = 25
--- End quote ---
It wants to start the thread but couldn't do it, because it already running.
How can I stop the thread and started again by opening a form?
marcov:
Maybe you are simply too fast shutting down after Terminate; Terminate tells thread to terminate, but the thread must actually terminate. (IOW come past the WHILE not terminated clause in the .execute)
You can wait for using .waitfor:
--- Code: ---if (assigned(scanner) then
begin
scanner.terminate; // signal request for termination
scanner.waitfor; // wait for termination
scanner.free; // deallocate
end;
--- End code ---
engkin:
Free does call both Terminate and WaitFor.
TScanner.Create is setting FreeOnTerminate to true, so change your code to:
--- Code: --- {$IFDEF LCLWinCE}
if scanner <> nil then
begin
scanner.Terminate;
scanner.WaitFor;
end;
{$ENDIF LCLWinCE}
--- End code ---
Or reuse the scanner thread by assigning nil to OnScan and OnError when you close a form, and re-assign them when you open a new form. Terminate the scanner thread only when the application closes.
mangakissa:
Thanks, Marcov and engkin.
I noticed it takes a lot of time to actually terminate the thread. If the customer wants I shall open the thread during the application. I'm glad it's working now.
Navigation
[0] Message Index