private async Task <FetchRowsResult> FetchRows(int page, CancellationToken cancellationToken)
        {
            try
            {
                CancellationToken linkedToken = BackgroundOperation.Register(this, cancellationToken);
                if (page == 1)
                {
                    await Task.Delay(500);
                }

                List <T> result = await RunAsync(() => FetchPage.Invoke(page, linkedToken));

                PageCount = result.Capacity;
                PageSource.UpdateSummaries();

                return(new FetchRowsResult(result.Cast <object>().ToArray()));
            }
            catch (TaskCanceledException)
            {
                return(new FetchRowsResult(new object[] { }));
            }
            finally
            {
                BackgroundOperation.UnRegister(this);
            }
        }
Example #2
0
        public async Task RefreshAll(MediaType mediaType)
        {
            if (MessageBoxService.ShowMessage(Properties.Resources.RefreshAllConfirm, Properties.Resources.RefreshAll,
                                              MessageButton.YesNo, MessageIcon.Question, MessageResult.No) != MessageResult.Yes)
            {
                return;
            }

            try
            {
                BeginProgress();
                IAsyncCommand     command           = this.GetAsyncCommand(x => x.RefreshAll(mediaType));
                CancellationToken cancellationToken = command.CancellationTokenSource.Token;
                BackgroundOperation.Register(Properties.Resources.RefreshAll, command.CancelCommand);

                switch (mediaType)
                {
                case MediaType.Movie:
                    foreach (MovieContainer container in App.Repository.Movies)
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            break;
                        }

                        await App.Repository.UpdateMovieAsync(container, cancellationToken);
                    }
                    break;

                case MediaType.Tv:
                    foreach (TvShowContainer container in App.Repository.TvShows)
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            break;
                        }

                        await App.Repository.UpdateTvShowAsync(container, cancellationToken);
                    }
                    break;

                case MediaType.Person:
                    foreach (PersonContainer container in App.Repository.People)
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            break;
                        }

                        await App.Repository.UpdatePersonAsync(container, cancellationToken);
                    }
                    break;
                }
            }
            finally
            {
                EndProgress();
                BackgroundOperation.UnRegister(Properties.Resources.RefreshAll);
            }
        }
Example #3
0
        /// <summary>
        /// Initializes <see cref="RssMixxxer"/>'s background maintenance.
        /// </summary>
        public void ForMyNeeds()
        {
            lock (_syncRoot)
            {
                if (_isRunning)
                {
                    _log.Warn("Failed attempt to start mixing feeds - another instance is already running.");
                    return;
                }

                _backgroundOperation = new BackgroundOperation(() =>
                {
                    var config      = _configProvider.ProvideConfig();
                    var sourceFeeds = config.SourceFeeds;

                    _log.Debug("Synchronizing {0} feeds", sourceFeeds.Length);

                    process_feeds(sourceFeeds);
                }, TimeSpan.FromSeconds(_configProvider.ProvideConfig().SyncInterval_Seconds));

                _backgroundOperation.Start();

                _isRunning = true;
            }
        }
Example #4
0
 /// <summary>
 /// This is the primary constructor for the Watchdog that allows all options to be specified.
 /// </summary>
 /// <param name="petTimeout">Once started, the watchdog must be <see cref="Pet"/> within this time span of it will be triggered</param>
 /// <param name="petMaxTimeUntilTriggered">Once the watchdog is started, it will be triggered after this time span even if
 /// <see cref="Pet"/> is called.</param>
 /// <param name="triggerCallback">This lambda is called when/if the watchdog is triggered.</param>
 /// <param name="autoStartOnFirstPet">If true, the watchdog will automatically be started on first <see cref="Pet"/>.
 /// Otherwise, <see cref="Monitor"/> has to be called to start the watchdog.</param>
 public Watchdog(TimeSpan petTimeout, TimeSpan petMaxTimeUntilTriggered, Action?triggerCallback, bool autoStartOnFirstPet)
 {
     AutoStartOnFirstPet      = autoStartOnFirstPet;
     PetTimeout               = petTimeout;
     PetMaxTimeUntilTriggered = petMaxTimeUntilTriggered;
     _triggerCallback         = triggerCallback;
     _backgroundOperation     = new BackgroundOperation(WatchdogOperationAsync);
 }
Example #5
0
        public void Dispose()
        {
            if (_backgroundOperation != null)
            {
                _log.Debug("Disposing feed mixxxer");

                _backgroundOperation.Dispose();
                _backgroundOperation = null;
            }
        }
Example #6
0
        public async Task UpdateItems(IList <object> items)
        {
            if (MessageBoxService.ShowMessage(Properties.Resources.RefreshSelectedConfirm, Properties.Resources.Refresh,
                                              MessageButton.YesNo, MessageIcon.Question, MessageResult.No) != MessageResult.Yes)
            {
                return;
            }

            try
            {
                IAsyncCommand     command           = this.GetAsyncCommand(x => x.UpdateItems(items));
                CancellationToken cancellationToken = command.CancellationTokenSource.Token;
                BackgroundOperation.Register(Properties.Resources.Refresh, command.CancelCommand);

                foreach (object item in items)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }

                    switch (item)
                    {
                    case MovieContainer movie:
                        await App.Repository.UpdateMovieAsync(movie, cancellationToken);

                        break;

                    case PersonContainer person:
                        await App.Repository.UpdatePersonAsync(person, cancellationToken);

                        break;

                    case TvShowContainer tvShow:
                        await App.Repository.UpdateTvShowAsync(tvShow, cancellationToken);

                        break;
                    }
                }
            }
            finally
            {
                BackgroundOperation.UnRegister(Properties.Resources.Refresh);
            }
        }
Example #7
0
        /// <summary>
        /// BackgroundTask constructor.
        /// </summary>
        /// <param name="operationText">The text to be displayed on the progress form</param>
        /// <param name="Operation">The operation to be performed in the background</param>
        public BackgroundTask(string operationText, BackgroundOperation Operation)
        {
            this.Operation    = Operation;
            this.progressForm = new WorkInProgressForm(operationText);

            Thread anOperationThread = new Thread(new ThreadStart(OperationThread));

            anOperationThread.Name = "BackgroundTask";
            anOperationThread.Start();

            progressForm.ShowDialog();
            anOperationThread.Join();
            GC.KeepAlive(progressForm);

            if ((operationException != null))
            {
                throw operationException;
            }
        }
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            List <Object>       workerArgs = e.Argument as List <object>;
            BackgroundOperation operation  = (BackgroundOperation)workerArgs[0];

            try
            {
                switch (operation)
                {
                case BackgroundOperation.ConnectProject:
                    ConnectProject();
                    break;

                case BackgroundOperation.OpenProject:
                    OpenProject((string)workerArgs[1]);
                    break;

                case BackgroundOperation.UpdateListsForUI:
                    UpdateListsForUI();
                    break;

                case BackgroundOperation.CopyDevices:
                    CopyDevice(backgroundWorker, e);
                    break;

                case BackgroundOperation.DownloadDevices:
                    DownloadDevices(backgroundWorker, e, (string)workerArgs[1]);
                    break;
                }

                e.Result = operation;
            }

            catch (Exception ex)
            {
                if (ex.Message.Contains("Error when calling method 'set_Value' of type 'Siemens.Engineering.MC.Drives.DriveParameter'") && currentParameter == 9700)
                {
                    //TODO Handling wrong safety Pass
                }
                e.Result = null;
            }
        }
        //BackgroundWorker Completed
        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                StatusMessage(e.Error.Message);
            }
            else if (e.Cancelled)
            {
                StatusMessage("Canceled");
                progressBar.Value = 100;
            }
            else if (e.Result == null)
            {
                StatusMessage("Something went wrong");
            }
            else
            {
                BackgroundOperation operation = (BackgroundOperation)e.Result;

                switch (operation)
                {
                case BackgroundOperation.ConnectProject:
                    if (myOpenness.tiaPortal is null || myOpenness.tiaPortal.Projects.Count <= 0)
                    {
                        StatusMessage("Connecting to TIA project FAILED, no open TIA project found.");
                    }
                    else
                    {
                        StatusMessage("Connected to TIA project " + myOpenness.tiaProject.Name);

                        StatusMessage("Getting Sinamics G devices ...");
                        if (!backgroundWorker.IsBusy)
                        {
                            backgroundWorker.RunWorkerAsync(new List <Object> {
                                BackgroundOperation.UpdateListsForUI
                            });
                        }
                    }
                    break;
Example #10
0
        void BackgroundOperationWork(object sender, DoWorkEventArgs args)
        {
            while (true)
            {
                BackgroundOperation current = Manager.CurrentBackgroundOperation;
                double progress             = Manager.BackgroundOperationProgress;

                if (double.IsNaN(progress))
                {
                    progress = 0;
                }

                int percent = (int)Math.Round(Math.Max(0, Math.Min(100, Manager.BackgroundOperationProgress)));
                int stackCount;

                if (current != null)
                {
                    if (backgroundActionLabel.Text != current.Name)
                    {
                        backgroundActionLabel.Text = current.Name;
                    }
                    if (backgroundActionLabel.ToolTipText != (current.Description ?? ""))
                    {
                        backgroundActionLabel.ToolTipText = (current.Description ?? "");
                    }
                }

                lock (BackgroundOperationStack)
                    stackCount = BackgroundOperationStack.Count;

                if (progress != progressBar.Value || stackCount > 0)
                {
                    BackgroundOperationWorker.ReportProgress(percent);
                }
                Thread.Sleep(100);
            }
        }
Example #11
0
        public async Task CreateImportView(MediaType mediaType)
        {
            ImportViewModel viewModel = ViewModelSource.Create <ImportViewModel>();

            viewModel.ParentViewModel = this;
            viewModel.MediaType       = mediaType;

            DialogService.ShowDialog(null, Properties.Resources.Import, "ImportView", viewModel);
            if (viewModel.SelectedMediaFileInfoList != null)
            {
                try
                {
                    BeginProgress();
                    IAsyncCommand     command           = this.GetAsyncCommand(x => x.CreateImportView(mediaType));
                    CancellationToken cancellationToken = command.CancellationTokenSource.Token;
                    BackgroundOperation.Register(Properties.Resources.ImportWizard, command.CancelCommand);

                    List <ContainerBase> newItems      = new List <ContainerBase>();
                    List <ContainerBase> existingItems = new List <ContainerBase>();

                    if (viewModel.MediaType == MediaType.Movie)
                    {
                        foreach (MediaFileInfo info in viewModel.SelectedMediaFileInfoList)
                        {
                            if (cancellationToken.IsCancellationRequested)
                            {
                                break;
                            }

                            SearchBase selectedItem = info.SelectedMediaItem as SearchBase;
                            if (selectedItem != null)
                            {
                                MovieContainer container = await App.Repository.GetMovieAsync(selectedItem.Id, cancellationToken);

                                if (container != null)
                                {
                                    container.Seen           = info.PersonalInfo.Seen;
                                    container.Favorite       = info.PersonalInfo.Favorite;
                                    container.Watchlist      = info.PersonalInfo.Watchlist;
                                    container.LocalPath      = info.PersonalInfo.LocalPath;
                                    container.PersonalRating = info.PersonalInfo.PersonalRating;

                                    if (container.IsAdded == false)
                                    {
                                        newItems.Add(container);
                                    }
                                    else
                                    {
                                        existingItems.Add(container);
                                    }
                                }
                            }
                        }

                        foreach (MovieContainer movieContainer in newItems)
                        {
                            App.Repository.Movies.Add(movieContainer);
                        }

                        foreach (MovieContainer movieContainer in existingItems)
                        {
                            App.Repository.Movies.Update(movieContainer);
                        }
                    }
                    else if (viewModel.MediaType == MediaType.Tv)
                    {
                        foreach (MediaFileInfo info in viewModel.SelectedMediaFileInfoList)
                        {
                            if (cancellationToken.IsCancellationRequested)
                            {
                                break;
                            }

                            SearchBase selectedItem = info.SelectedMediaItem as SearchBase;
                            if (selectedItem != null)
                            {
                                TvShowContainer container = await App.Repository.GetTvShowAsync(selectedItem.Id, cancellationToken);

                                if (container != null)
                                {
                                    container.Seen           = info.PersonalInfo.Seen;
                                    container.Favorite       = info.PersonalInfo.Favorite;
                                    container.Watchlist      = info.PersonalInfo.Watchlist;
                                    container.LocalPath      = info.PersonalInfo.LocalPath;
                                    container.PersonalRating = info.PersonalInfo.PersonalRating;

                                    if (container.IsAdded == false)
                                    {
                                        newItems.Add(container);
                                    }
                                    else
                                    {
                                        existingItems.Add(container);
                                    }
                                }
                            }
                        }

                        foreach (TvShowContainer tvShowContainer in newItems)
                        {
                            App.Repository.TvShows.Add(tvShowContainer);
                        }

                        foreach (TvShowContainer tvShowContainer in existingItems)
                        {
                            App.Repository.TvShows.Update(tvShowContainer);
                        }
                    }
                    else if (viewModel.MediaType == MediaType.Person)
                    {
                        foreach (MediaFileInfo info in viewModel.SelectedMediaFileInfoList)
                        {
                            if (cancellationToken.IsCancellationRequested)
                            {
                                break;
                            }

                            SearchBase selectedItem = info.SelectedMediaItem as SearchBase;
                            if (selectedItem != null)
                            {
                                PersonContainer container = await App.Repository.GetPersonAsync(selectedItem.Id, cancellationToken);

                                if (container != null)
                                {
                                    container.Favorite  = info.PersonalInfo.Favorite;
                                    container.LocalPath = info.PersonalInfo.LocalPath;

                                    if (container.IsAdded == false)
                                    {
                                        newItems.Add(container);
                                    }
                                    else
                                    {
                                        existingItems.Add(container);
                                    }
                                }
                            }
                        }

                        foreach (PersonContainer personContainer in newItems)
                        {
                            App.Repository.People.Add(personContainer);
                        }

                        foreach (PersonContainer personContainer in existingItems)
                        {
                            App.Repository.People.Update(personContainer);
                        }
                    }
                }
                finally
                {
                    EndProgress();
                    BackgroundOperation.UnRegister(Properties.Resources.ImportWizard);
                }
            }
        }