Tuesday, August 25, 2009

Delphi modal form not closing

I just spent a frustrating afternoon trying to fix a problem in someone else's code written in Delphi 5 (I know it's ancient, but there are good reasons why it hasn't been upgraded).
I had a modal form that was displayed using ShowModal and when a comms error occured I set ModalResult to mrAbort expecting the form to close, but it wouldn't.
The form uses a serial port component (see http://comport.sf.net/ ) and has an OnRxChar callback on the serial port component set up to process characters. The OnRxChar callback is set to a method of the modal form. It turns out that the window won't close until you "unhook" the callback so I had to do the following to get it to close :

// Close the form when an error occurs
procedure TGetFilesForm.Stop;
begin
// Must get rid of the rx callback, otherwise
// the form won't close
if _comport <> nil then
_comport.OnRxChar := nil;
ModalResult := mrAbort;
end;

No comments:

Post a Comment