Changed the retry function in PithosClient to use the TPL
[pithos-ms-client] / trunk / Pithos.Core / JobQueue.cs
1 // -----------------------------------------------------------------------
2 // <copyright file="JobQueue.cs" company="Microsoft">
3 // TODO: Update copyright text.
4 // </copyright>
5 // -----------------------------------------------------------------------
6
7 using System.Collections.Concurrent;
8 using System.Threading;
9 using System.Threading.Tasks;
10
11 namespace Pithos.Core
12 {
13     using System;
14     using System.Collections.Generic;
15     using System.Linq;
16     using System.Text;
17
18     /// <summary>
19     /// TODO: Update summary.
20     /// </summary>
21     public class JobQueue
22     {
23         private readonly BlockingCollection<Action> _statusUpdateQueue = new BlockingCollection<Action>();
24         private CancellationToken _cancellationToken;
25         
26
27         public void Start(CancellationToken token)
28         {
29             _cancellationToken = token;
30             Task.Factory.StartNew(ProcessUpdates, _cancellationToken);
31         }
32
33         private void ProcessUpdates()
34         {
35             foreach (var action in _statusUpdateQueue.GetConsumingEnumerable())
36             {
37                 action();
38             }
39         }
40
41         public void Add(Action action)
42         {
43             _statusUpdateQueue.Add(action);
44         }
45
46         public void Stop()
47         {
48             _statusUpdateQueue.CompleteAdding();
49         }
50        
51     }
52 }