views:

91

answers:

5

I have a FileSystemWatcher that react on the Changed event.

I want to open the file, read its content display it in a textbox and hide the popup that has been created after 1 sec. The code almost work but something fail when hiding the popup.

Here is a snippet of the code :

       txtLog.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate() {
            this.txtLog.Text = dataToDisplay;
            extendedNotifyIcon_OnShowWindow();
            Thread threadToClosePopup = new Thread(new ThreadStart((Action)delegate() { 
                Thread.Sleep(1000); 

                extendedNotifyIcon_OnHideWindow();
       }));
            threadToClosePopup.Start();
        });

As you can see, I use the Invoke to set the text because the event is in a different thread (FileSystemWatcher). But to hide the windows the extendedNotifyIcon_OnHideWindow() is not executed in the thread of the GUI. How can I execute it in the GUI thread?

+1  A: 

This will give you the Window dispatcher:

Dispatcher.CurrentDispatcher

As long as you get it on the windows thread.

ozczecho
+1  A: 

Use Control.Invoke

   txtLog.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate() {
        this.txtLog.Text = dataToDisplay;
        extendedNotifyIcon_OnShowWindow();
        Thread threadToClosePopup = new Thread(new ThreadStart((Action)delegate() { 
            Thread.Sleep(1000); 

            extendedNotifyIcon.Invoke(extendedNotifyIcon_OnHideWindow);
   }));
        threadToClosePopup.Start();
    });
Alex Reitbort
+4  A: 

To execute the extendedNotifyIcon_OnHideWindow method on the GUI thread use the Dispatcher as you did to show it.

Thread threadToClosePopup = new Thread(new ThreadStart((Action)delegate() { 
  Thread.Sleep(1000); 
  txtLog.Dispatcher.Invoke(
    DispatcherPriority.Normal,
    (Action)() => extendedNotifyIcon_OnHideWindow());
}));
JaredPar
+1  A: 

Just wrap extendedNotifyIcon_OnHideWindow(); into a Dispatcher.Invoke()

But I would rather do it (all im XAML) using an Animation and with an EvenTrigger that triggers upon the TimeLine.Completed event.

bitbonk
I am really new to WPF, just needed to modify an existing code. The code in the XAML popup and in fact pop down when the mouse leave the container. But, now I need to add something to pop it from code and pop down from code without deleting the initial behavior. +1 for the idea
Daok
A: 

The problem is that extendedNotifyIcon_OnHideWindow is being executed on a thread other than the UI thread. You will need to use the Dispatcher for that part as well. Also, I would not create a dedicated thread just to wait one second. You could easily refactor that part into a System.Threading.Timer. Here is my refactored version.

txtLog.Dispatcher.Invoke(
    (Action)(() =>
        {
            txtLog.Text = dataToDisplay;
            extendedNotifyIcon_OnShowWindow(); 
            new Timer(
                (state) =>
                {
                    button1.Dispatcher.BeginInvoke(
                        (Action)(() =>
                            {
                              extendedNotifyIcon_OnHideWindow(); 
                            }), null);
                }, null, 1000, Timeout.Infinite);
        }));
Brian Gideon