开发者

Continuing several tasks

I am using TPL to perform two tasks serially on a background thread in an MVVM app. While the tasks are running, the app displays a Progress dialog. So, my MVVM command's Execute() method first raises an ImageProcessingStarting event in the main view model. The view responds to the event by displaying the Progress dialog. The command then launches the first task, continues with the second task, and performs the final 'continue with' by raising an ImageProcessingEnding event in the main view model. The view responds to the event by closing the Progress dialog. The code is below.

Both background ta开发者_C百科sks are executing properly, but the Progress dialog is closing early, after the first task completes, instead of after the second one. I am hoping someone can tell me why, and how to fix the problem. Thanks for your help.


public void Execute(object parameter)
{
    ...

    // Announce that image processing is starting
    m_ViewModel.RaiseImageProcessingStartingEvent();

    // Set up a cancellation token source
    var tokenSource = new CancellationTokenSource();
    m_ViewModel.ProgressDialogViewModel.TokenSource = tokenSource;

    // Background Task #1: Add time stamps to files
    var task = Task.Factory.StartNew(() => this.AddTimeStampsToFiles(fileList, progressDialogViewModel));

    /* The Progress dialog is closing at this point! */

    // Background Task #2: Resequence files
    task.ContinueWith(t => this.ResequenceFiles(fileList, progressDialogViewModel));

    /* The Progress dialog should close at this point. */

    // Announce that image processing is finished
    task.ContinueWith(t => m_ViewModel.RaiseImageProcessingEndingEvent(), TaskScheduler.FromCurrentSynchronizationContext());
}


According to MSDN

The Task.ContinueWith Method

Creates a continuation that executes asynchronously when the target Task completes.

Which means that when the main task finishes, the other items in your two ContinueWith calls will run in parallel with one another.

To demonstrate this, you use the code below:

System.Threading.Tasks.Task task = new System.Threading.Tasks.Task(() => Console.WriteLine("1"));
task.ContinueWith((t) => { System.Threading.Thread.Sleep(1000); Console.WriteLine("2"); });
task.ContinueWith((t) => Console.WriteLine("3"));

The Output Window will then read:

1
3
2

To help you out in your question, I have always used the System.ComponentModel.BackgroundWorker to run tasks serially. There are probably better ways, but this works for me for now.

public void Execute(object parameter)
{
    BackgroundWorker bgW = new BackgroundWorker();

    bgW.DoWork += (s, args) =>
    {
        AddTimeStampsToFiles(fileList, progressDialogViewModel);
        ResequenceFiles(fileList, progressDialogViewModel);
    };

    bgW.RunWorkerCompleted += (s, args) =>
    {
        m_ViewModel.RaiseImageProcessingEndingEvent();
    }; 

    m_ViewModel.RaiseImageProcessingStartingEvent();
    bgW.RunWorkerAsync();
}

For this to work, you may need to pass the fileList and progressDialogViewModel values in to the bgW.RunWorkerAsync() method.

For multiple values, I typically use a Dictionary<string, object> object so I can refer to the values by name.

Hope this helps.


Found my answer. In my original code, both the second and third tasks were continuations of the first task. I changed my code to create separate tasks for the original task (taskOne) and the continuation tasks (taskTwo and taskThree). Then, taskTwo continues taskOne, and taskThree continues taskTwo, like this:

// Background Task #1: Add time stamps to files
var taskOne = Task.Factory.StartNew(() => AddTimeStampsToFiles(fileList, progressDialogViewModel));

// Background Task #2: Resequence files
var taskTwo = taskOne.ContinueWith(t => this.ResequenceFiles(fileList, progressDialogViewModel));

// Announce that image processing is finished
var taskThree = taskTwo.ContinueWith(t => m_ViewModel.RaiseImageProcessingEndingEvent(), TaskScheduler.FromCurrentSynchronizationContext());

I have accepted Fatty's answer, since it does present a viable alternative. However, I am using the approach in this answer in my application, since I am using TPL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜