private Task StartService(CancellationToken cancellationToken)
        {
            var dispatcher = Application.Current.MainWindow.Dispatcher;

            return(Task.Factory.StartNew(() =>
            {
                do
                {
                    // Gets the next display location in the screen
                    int nextLocation = FindNextLocation();

                    if (nextLocation > -1)
                    {
                        NotifyMessage msg = null;
                        //  Retrieve the message from the queue
                        if (QueuedMessages.TryDequeue(out msg))
                        {
                            //  construct a View Model and binds it to the Popup Window
                            var viewModel = new NotifyMessageViewModel(msg,
                                                                       DisplayLocations[nextLocation],
                                                                       () => DisplayMessages[nextLocation] = null); // Free the display location when the popup window is closed
                            DisplayMessages[nextLocation] = viewModel;

                            //  Use Application.Current.MainWindow.Dispatcher to switch back to the UI Thread to create popup window
                            dispatcher.BeginInvoke(
                                new MethodInvoker(() =>
                            {
                                var window = new NotifyMessageWindow()
                                {
                                    Owner = Application.Current.MainWindow,
                                    DataContext = viewModel,
                                    ShowInTaskbar = false
                                };
                                window.Show();
                            }), DispatcherPriority.Background);
                        }
                    }
                    Thread.Sleep(1000);
                } while (QueuedMessages.Count > 0 && !cancellationToken.IsCancellationRequested);

                Stop();
            }));
        }
 public void EnqueueMessage(NotifyMessage msg)
 {
     QueuedMessages.Enqueue(msg);
     Start();
 }
Exemple #3
0
 public NotifyMessageViewModel(NotifyMessage content, AnimatedLocation location, Action closedAction)
 {
     this._content      = content;
     this._location     = location;
     this._closedAction = closedAction;
 }