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

                return(Task.Factory.StartNew(async() =>
                {
                    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
                                await dispatcher.BeginInvoke(
                                    new MethodInvoker(() =>
                                {
                                    var window = new AlertDialog()
                                    {
                                        Owner = Application.Current.MainWindow,
                                        DataContext = viewModel,
                                        ShowInTaskbar = false
                                    };
                                    window.Show();
                                }), DispatcherPriority.Background);
                            }
                        }
                        await Task.Delay(1000);
                    } while (QueuedMessages.Count > 0 && !cancellationToken.IsCancellationRequested);

                    Stop();
                }));
            }
            catch
            {
                return(null);
            }
        }
 public void EnqueueMessage(NotifyMessage msg)
 {
     QueuedMessages.Enqueue(msg);
     Start();
 }
 public NotifyMessageViewModel(NotifyMessage content, AnimatedLocation location, Action closedAction)
 {
     this.content      = content;
     this.location     = location;
     this.closedAction = closedAction;
 }