How to implement an async sequential worker in C#
<p>In C# .Net 4, I want to implement an async sequential worker, that queues and performs Actions one by one. However two or more sequential workers can run concurrently and independent from each other.<br>
What would be the best implementation regarding performance and resource usage?! (I know that a producer-consumer pattern would be suitable for this but how to implement it?!)</p>

<p>Below I put the code of the empty class and then a naive implementation for it.  </p>

<pre><code>public class SequentialWorker
{
    public void DoAsync(Action action1)
    {
        // queue and run action1 after previous actions, on a seperate thread
    }
}
</code></pre>

<p>Naive implementation:</p>

<pre><code>public class SequentialWorker
{
    private Task task;
    private readonly Queue&lt;Action&gt; actions = new Queue&lt;Action&gt;();

    public void DoAsync(Action action1)
    {
        lock (actions)
        {
            actions.Enqueue(action1);
            if (task == null || task.IsCompleted) // if completed or not created, create a new task
            {
                task = Task.Factory.StartNew(DoActions);
            }
        }
    }

    private void DoActions()
    {
        while (true)
        {
            try
            {
                Action action;
                lock (actions)
                {
                    if (!actions.Any())
                        return;
                    action = actions.Dequeue();
                }

                action();
            }
            catch (Exception e)
            {
                //handle e
            }
        }
    }
}
</code></pre>