root / trunk / Pithos.Core / JobQueue.cs @ cd7c162f
History | View | Annotate | Download (1.5 kB)
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 |
Parallel.ForEach(_statusUpdateQueue.GetConsumingEnumerable(), |
36 |
new ParallelOptions{MaxDegreeOfParallelism=1,CancellationToken=_cancellationToken}, |
37 |
action => action()); |
38 |
foreach (var action in _statusUpdateQueue.GetConsumingEnumerable()) |
39 |
{ |
40 |
action(); |
41 |
} |
42 |
} |
43 |
|
44 |
public void Add(Action action) |
45 |
{ |
46 |
_statusUpdateQueue.Add(action); |
47 |
} |
48 |
|
49 |
public void Stop() |
50 |
{ |
51 |
_statusUpdateQueue.CompleteAdding(); |
52 |
} |
53 |
|
54 |
} |
55 |
} |