Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with threading a desktop alert

Status
Not open for further replies.

DjangMan

Programmer
Jun 1, 2001
1,783
CA
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:
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);
 
you are violating rule #1 with threading:

Your background thread may not interact directly with the GUI!

rethink your GUI design, you want to alert the user, but you want to continue processing, right?
Then DONT show a modal dialog box as this will interrupt the main thread, search for an alternative way to alert the user...


/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Hmmm - I must be doing something wrong. The demo for the TJvDesktopAlert allows me to create the alert and then I can call showmessage('Hello') and while the dialog is displayed the alert will fade out by itself.



 
The 'Synchronize' marshals the call back to the main thread so you are not really achieving anything by using a thread here.

You need to make the alert.Execute non-modal.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top