title: Show a notification window then fade it out, but pause the animation on mouse over
body: <p>I'm normally an ASP.NET programmer, so I don't have much experience with WPF. I'm trying to create a notification window that opens on the screen in response to some server side event. I want it to behave similarly to the notifications given by Microsoft Outlook. The window should show for 5 seconds, then begin fading out which should last for 5 seconds, at which point the window should close. If the user mouses over the window, the windowTimer or the fade out animation should pause depending on how much time has elapsed. This works fine in the first 5 seconds of execution. However, once the fade out animation starts, it doesn't pause by mousing over. My question is: Why isn't my code pausing the animation when the mouse enters?</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      Timer windowTimer; //System.Timers.Timer
      AnimationClock clock;
      bool fadingOut = false;
  
      public MainWindow()
      {
          InitializeComponent();
      }
  
      protected override void OnContentRendered(EventArgs e)
      {
          base.OnContentRendered(e);
          windowTimer = new Timer(5000);//5 seconds
          windowTimer.Start();
          windowTimer.Elapsed += new ElapsedEventHandler(windowTimer_Elapsed);
      }
  
      protected override void OnMouseEnter(MouseEventArgs e)
      {
          base.OnMouseEnter(e);
          if (!fadingOut)
          {
              windowTimer.Stop();
          }
          if (clock != null)
          {
              clock.Controller.Pause();
          }
      }
  
      protected override void OnMouseLeave(MouseEventArgs e)
      {
          base.OnMouseLeave(e);
          if (!fadingOut)
          {
              windowTimer.Start();
          }
          if (clock != null)
          {
              clock.Controller.Resume();
          }
      }
  
      private void windowTimer_Elapsed(object sender, ElapsedEventArgs e)
      {
          windowTimer.Elapsed -= windowTimer_Elapsed;
          Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
              new Action(() =&gt; BeginFadeout()));
      }
  
      private void BeginFadeout()
      {
          windowTimer.Stop();
          fadingOut = true;
          DoubleAnimation fadeoutanim = new DoubleAnimation(0,
              (Duration)TimeSpan.FromSeconds(5));
          fadeoutanim.Completed += (s, _) =&gt; this.Close();
          clock = fadeoutanim.CreateClock();
          this.BeginAnimation(UIElement.OpacityProperty, fadeoutanim);
      }
  }
  </code></pre>