I've got an application that imports transactions into another system. As these transactions are imported I want to display a TJvDesktopAlert (from the Jedi library). What I found was that my program stopped while the alert was displayed and continued on when the alert vanished or was closed. So I figured this is a good time to look at a thread.
The idea was to spawn a thread that displays the alert. My main processing thread can continue and I don't really care about the alert after I spawn it.
The problem is that it doesn't seem to work - my main thread finishes importing transactions and then I get a long stack of alerts. It's as if the threads are waiting for something. I've included an Application.ProcessMessages in my loop but that didn't seem to help.
Here's my code:
Called with:
The idea was to spawn a thread that displays the alert. My main processing thread can continue and I don't really care about the alert after I spawn it.
The problem is that it doesn't seem to work - my main thread finishes importing transactions and then I get a long stack of alerts. It's as if the threads are waiting for something. I've included an Application.ProcessMessages in my loop but that didn't seem to help.
Here's my code:
Code:
TMyAlertThread = class(TThread)
protected
procedure Execute; override;
public
Alert: TJvDesktopAlert;
Shown: boolean;
procedure ShowMe;
end;
procedure TMyAlertThread.ShowMe;
begin
if Assigned(Alert) then begin
if not(Shown) then begin
Alert.Execute;
Shown := True;
end;
end;
end;
procedure TMyAlertThread.Execute;
begin
FreeOnTerminate := True;
Synchronize(ShowMe);
while Alert.Showing and (not terminated) do;
end;
Code:
procedure TdmDB.ShowAlert(aHeader, aBody: string; aDuration: Cardinal = 5000);
var
DA: TJvDesktopAlert;
FOptions:TJvDesktopAlertOptions;
AlertThread: TMyAlertThread;
begin
AlertThread := TMyAlertThread.Create(True);
AlertThread.Shown := False;
FOptions := [];
DA := TJvDesktopAlert.Create(nil);
with DA do begin
AutoFree := True;
HeaderText := aHeader;
MessageText := aBody;
Font.Style := Font.Style - [fsBold];
Include(FOptions,daoCanClose);
DA.Options := FOptions;
StyleHandler.DisplayDuration := 5000;
Location.Position := dapBottomRight;
end;
AlertThread.Alert := DA;
AlertThread.Resume;
end;
Called with:
Code:
dmDB.ShowAlert('Order imported','Order ID:'+aOrderID+' imported as: '+OrderNumber,2000);