コード例 #1
0
        /// <summary>
        /// Clone this instance and return one which only allows to set "ProgressInfo"
        /// </summary>
        /// <returns>A cloned instance</returns>
        public TrackProgressParameters CloneWithoutIncrements()
        {
            TrackProgressParameters copy
                = new TrackProgressParameters(
                      Background.TrackProgressMode.PerformWithoutIncrements,
                      new TrackProgressWithoutIncrementsWrapper(this.TrackProgress),
                      this.CancellationToken);

            return(copy);
        }
コード例 #2
0
 /// <summary>
 /// Runs a specified action and shows a dialog with a cancel button, if the action takes longer than 100msec
 /// </summary>
 /// <param name="action">The action</param>
 /// <param name="externalProgress">Used when integrated in "outer" context</param>
 /// <returns>true if successful</returns>
 public bool RunWithCancelDialog(Action <TrackProgressParameters> action, TrackProgressParameters externalProgress = null)
 {
     return(RunWithCancelDialog((parameters) => { action(parameters); return true; }, externalProgress));
 }
コード例 #3
0
 /// <summary>
 /// Runs a specified action and shows a dialog with a cancel button, if the action takes longer than 100msec
 /// </summary>
 /// <typeparam name="T_Result">The type of the result</typeparam>
 /// <param name="action">The action</param>
 /// <param name="externalProgress">Used when integrated in "outer" context</param>
 /// <returns>The result</returns>
 public T_Result RunWithCancelDialog <T_Result>(Func <TrackProgressParameters, T_Result> action, TrackProgressParameters externalProgress = null)
 {
     return(RunWithCancelDialog(action, null, null, externalProgress));
 }
コード例 #4
0
        /// <summary>
        /// Runs a specified action and shows a dialog with a cancel button, if the action takes longer than 100msec
        /// </summary>
        /// <typeparam name="T_Result">The type of the result</typeparam>
        /// <param name="action">The action</param>
        /// <param name="caption">The caption, may be null</param>
        /// <param name="description">The description, may be null</param>
        /// <param name="externalProgress">Used when integrated in "outer" context</param>
        /// <returns>The result</returns>
        public T_Result RunWithCancelDialog <T_Result>(Func <TrackProgressParameters, T_Result> action, string caption, string description, TrackProgressParameters externalProgress = null)
        {
            if (_cancelMode)
            {
                return(default(T_Result));
            }

            if (_cancelDialogActive)
            {
                return(action(new TrackProgressParameters(TrackProgressMode.PerformWithIncrementsAndSetMaximum)));
            }
            try
            {
                _cancelDialogActive = true;

                if (externalProgress != null)
                {
                    try
                    {
                        return(action(externalProgress));
                    }
                    catch (OperationCanceledException)
                    {
                        return(default(T_Result));
                    }
                }

                T_Result returnValue = default(T_Result);
                if (Thread.CurrentThread.ManagedThreadId != _uiThreadId)
                {
                    _uiContext.Send(
                        (o) =>
                    {
                        returnValue = RunWithCancelDialog(action, caption, description);
                    }, null);
                    return(returnValue);
                }

                Task     task       = null;
                T_Result taskResult = default(T_Result);
                try
                {
                    var cts = new CancellationTokenSource();
                    var trackProgressParams = new TrackProgressParameters(TrackProgressMode.PerformWithIncrementsAndSetMaximum, cts.Token);
                    task = new Task(() => taskResult = action(trackProgressParams));
                    task.Start();
                    task.Wait(250);
                    if (task.IsCanceled || task.IsFaulted)
                    {
                        if (!_cancelMode && !task.IsCanceled && !cts.IsCancellationRequested && task.Exception != null)
                        {
                            throw task.Exception;
                        }

                        return(default(T_Result));
                    }
                    else if (task.IsCompleted)
                    {
                        return(taskResult);
                    }

                    var wnd = new Windows.WaitForBackgroundActionDialog(trackProgressParams.TrackProgress, cts, task);
                    if (caption != null)
                    {
                        wnd.WindowStyle = System.Windows.WindowStyle.ToolWindow;
                        wnd.Title       = caption;
                    }
                    if (description != null)
                    {
                        wnd.Description.Text = description;
                    }
                    if (_viewManager != null)
                    {
                        _viewManager.RaiseBeforeModalDialog(wnd);
                        _viewManager.RaiseWindowCreated(wnd);
                    }
                    try
                    {
                        wnd.ShowDialog();
                    }
                    finally
                    {
                        if (_viewManager != null)
                        {
                            _viewManager.RaiseAfterModalDialog(wnd);
                        }
                    }
                }
                catch (OperationCanceledException)
                {
                }
                finally
                {
                    if (task != null && task.IsCompleted && !task.IsFaulted)
                    {
                        returnValue = taskResult;
                    }
                }
                if (task != null && task.Exception != null)
                {
                    if (task.Exception.InnerExceptions.Any(ex => ex.GetType() != typeof(OperationCanceledException)))
                    {
                        throw task.Exception;
                    }
                }
                return(returnValue);
            }
            finally
            {
                _cancelDialogActive = false;
            }
        }