Creating a execution queue by using Task.ContinueWith?

Viewed 8253

I have several actions that I want to execute in the background, but they have to be executed synchronously one after the other.

I was wondering if it's a good idea to use the Task.ContinueWith method to achieve this. Do you foresee any problems with this?

My code looks something like this:

private object syncRoot =new object();
private Task latestTask;

public void EnqueueAction(System.Action action)
{
    lock (syncRoot)
    {
        if (latestTask == null)
            latestTask = Task.Factory.StartNew(action);
        else
            latestTask = latestTask.ContinueWith(tsk => action());
    }
}
3 Answers
Related