/// <summary>
        /// Constructs a new background task based on the specified method and optional state object.
        /// </summary>
        /// <remarks>
        /// The task is not executed until the <see cref="Run"/> method is called.
        /// </remarks>
        /// <param name="method">The method to run in the background.</param>
        /// <param name="supportsCancel">Indicates whether the task supports cancellation or not.</param>
        /// <param name="userState">Optional state to be passed to the background method.</param>
        public BackgroundTask(BackgroundTaskMethod method, bool supportsCancel, object userState)
        {
            Platform.CheckForNullReference(method, "method");
            _method    = method;
            _userState = userState;

            _backgroundWorker = new BackgroundWorker();
            _backgroundWorker.WorkerReportsProgress      = true;
            _backgroundWorker.WorkerSupportsCancellation = supportsCancel;
            _backgroundWorker.DoWork             += new DoWorkEventHandler(BackgroundWorkerDoWork);
            _backgroundWorker.ProgressChanged    += new ProgressChangedEventHandler(BackgroundWorkerProgressChanged);
            _backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerRunWorkerCompleted);
        }
Exemple #2
0
        /// <summary>
        /// Constructs a new background task based on the specified method and optional state object.
        /// </summary>
        /// <remarks>
        /// The task is not executed until the <see cref="Run"/> method is called.
        /// </remarks>
        /// <param name="method">The method to run in the background.</param>
        /// <param name="supportsCancel">Indicates whether the task supports cancellation or not.</param>
        /// <param name="userState">Optional state to be passed to the background method.</param>
        public BackgroundTask(BackgroundTaskMethod method, bool supportsCancel, object userState)
        {
            Platform.CheckForNullReference(method, "method");
            _method    = method;
            _userState = userState;

            _backgroundWorker = new BackgroundWorker
            {
                WorkerReportsProgress      = true,
                WorkerSupportsCancellation = supportsCancel
            };
            _backgroundWorker.DoWork             += BackgroundWorkerDoWork;
            _backgroundWorker.ProgressChanged    += BackgroundWorkerProgressChanged;
            _backgroundWorker.RunWorkerCompleted += BackgroundWorkerRunWorkerCompleted;
        }
        /// <summary>
        /// Creates and executes a new <see cref="BackgroundTask"/> based on the specified arguments.
        /// </summary>
        /// <param name="method">The method to run in the background.</param>
        /// <param name="supportsCancel">Indicates whether the task supports cancellation or not.</param>
        /// <param name="terminateHandler">Method that will be called when the task terminates.</param>
        /// <param name="progressHandler">Optional method to handle progress updates, may be null.</param>
        /// <param name="userState">Optional state to be passed to the background task, may be null.</param>
        /// <returns>A running <see cref="BackgroundTask"/> object.</returns>
        public static BackgroundTask CreateAndRun(
            BackgroundTaskMethod method,
            bool supportsCancel,
            EventHandler <BackgroundTaskTerminatedEventArgs> terminateHandler,
            EventHandler <BackgroundTaskProgressEventArgs> progressHandler,
            object userState)
        {
            Platform.CheckForNullReference(method, "method");
            Platform.CheckForNullReference(terminateHandler, "terminateHandler");

            BackgroundTask task = new BackgroundTask(method, supportsCancel, userState);

            task.Terminated += terminateHandler;
            if (progressHandler != null)
            {
                task.ProgressUpdated += progressHandler;
            }
            task.Run();
            return(task);
        }
        /// <summary>
        /// Constructs a new background task based on the specified method.
        /// </summary>
		/// <remarks>
		/// The task is not executed until the <see cref="Run"/> method is called.
		/// </remarks>
		/// <param name="method">The method to run in the background.</param>
        /// <param name="supportsCancel">Indicates whether the task supports cancellation or not.</param>
        public BackgroundTask(BackgroundTaskMethod method, bool supportsCancel)
            : this(method, supportsCancel, null)
        {
        }
        /// <summary>
        /// Constructs a new background task based on the specified method and optional state object.
        /// </summary>
        /// <remarks>
		/// The task is not executed until the <see cref="Run"/> method is called.
		/// </remarks>
        /// <param name="method">The method to run in the background.</param>
        /// <param name="supportsCancel">Indicates whether the task supports cancellation or not.</param>
        /// <param name="userState">Optional state to be passed to the background method.</param>
        public BackgroundTask(BackgroundTaskMethod method, bool supportsCancel, object userState)
        {
            Platform.CheckForNullReference(method, "method");
            _method = method;
            _userState = userState;

            _backgroundWorker = new BackgroundWorker();
            _backgroundWorker.WorkerReportsProgress = true;
            _backgroundWorker.WorkerSupportsCancellation = supportsCancel;
            _backgroundWorker.DoWork += new DoWorkEventHandler(BackgroundWorkerDoWork);
            _backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorkerProgressChanged);
            _backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerRunWorkerCompleted);
        }
        /// <summary>
        /// Creates and executes a new <see cref="BackgroundTask"/> based on the specified arguments.
        /// </summary>
        /// <param name="method">The method to run in the background.</param>
        /// <param name="supportsCancel">Indicates whether the task supports cancellation or not.</param>
        /// <param name="terminateHandler">Method that will be called when the task terminates.</param>
        /// <param name="progressHandler">Optional method to handle progress updates, may be null.</param>
        /// <param name="userState">Optional state to be passed to the background task, may be null.</param>
        /// <returns>A running <see cref="BackgroundTask"/> object.</returns>
        public static BackgroundTask CreateAndRun(
            BackgroundTaskMethod method,
            bool supportsCancel,
            EventHandler<BackgroundTaskTerminatedEventArgs> terminateHandler,
            EventHandler<BackgroundTaskProgressEventArgs> progressHandler,
            object userState)
        {
            Platform.CheckForNullReference(method, "method");
            Platform.CheckForNullReference(terminateHandler, "terminateHandler");

            BackgroundTask task = new BackgroundTask(method, supportsCancel, userState);
            task.Terminated += terminateHandler;
            if (progressHandler != null)
            {
                task.ProgressUpdated += progressHandler;
            }
            task.Run();
            return task;
        }
 /// <summary>
 /// Constructs a new background task based on the specified method.
 /// </summary>
 /// <remarks>
 /// The task is not executed until the <see cref="Run"/> method is called.
 /// </remarks>
 /// <param name="method">The method to run in the background.</param>
 /// <param name="supportsCancel">Indicates whether the task supports cancellation or not.</param>
 public BackgroundTask(BackgroundTaskMethod method, bool supportsCancel)
     : this(method, supportsCancel, null)
 {
 }
		/// <summary>
		/// Constructs a new background task based on the specified method and optional state object.
		/// </summary>
		/// <remarks>
		/// The task is not executed until the <see cref="Run"/> method is called.
		/// </remarks>
		/// <param name="method">The method to run in the background.</param>
		/// <param name="supportsCancel">Indicates whether the task supports cancellation or not.</param>
		/// <param name="userState">Optional state to be passed to the background method.</param>
		public BackgroundTask(BackgroundTaskMethod method, bool supportsCancel, object userState)
		{
			Platform.CheckForNullReference(method, "method");
			_method = method;
			_userState = userState;

			_backgroundWorker = new BackgroundWorker
									{
										WorkerReportsProgress = true,
										WorkerSupportsCancellation = supportsCancel
									};
			_backgroundWorker.DoWork += BackgroundWorkerDoWork;
			_backgroundWorker.ProgressChanged += BackgroundWorkerProgressChanged;
			_backgroundWorker.RunWorkerCompleted += BackgroundWorkerRunWorkerCompleted;
		}