Revision 303596f9

b/trunk/Pithos.Core/Agents/AsyncManualResetEvent.cs
1
// -----------------------------------------------------------------------
2
// <copyright file="AsyncManualResetEvent.cs" company="Microsoft">
3
// TODO: Update copyright text.
4
// </copyright>
5
// -----------------------------------------------------------------------
6

  
7
using System.Threading;
8
using System.Threading.Tasks;
9

  
10
namespace Pithos.Core.Agents
11
{
12
    using System;
13
    using System.Collections.Generic;
14
    using System.Linq;
15
    using System.Text;
16

  
17
    /// <summary>
18
    /// TODO: Update summary.
19
    /// </summary>
20
    public class AsyncManualResetEvent
21
    {
22
        private volatile TaskCompletionSource<bool> _tcs;
23

  
24
        public AsyncManualResetEvent()
25
        {
26
            _tcs = new TaskCompletionSource<bool>();
27
        }
28

  
29
        public AsyncManualResetEvent(bool signalled)
30
        {            
31
            _tcs = new TaskCompletionSource<bool>();
32
            if (signalled)
33
                _tcs.SetResult(true);
34
        }
35
        public Task WaitAsync() { return _tcs.Task; }
36

  
37
        public void Wait()
38
        {            
39
            _tcs.Task.Wait();
40
        }
41

  
42
        public void Set() { _tcs.TrySetResult(true); }
43

  
44
        public void Reset()
45
        {
46
            while (true)
47
            {
48
                var tcs = _tcs;
49
                if (!tcs.Task.IsCompleted ||
50
                    Interlocked.CompareExchange(ref _tcs, new TaskCompletionSource<bool>(), tcs) == tcs)
51
                    return;
52
            }
53
        }
54
    }
55
}
b/trunk/Pithos.Core/Agents/NetworkAgent.cs
65 65
        readonly ConcurrentDictionary<string,DateTime> _deletedFiles=new ConcurrentDictionary<string, DateTime>();
66 66

  
67 67

  
68
        private readonly ManualResetEventSlim _pauseAgent = new ManualResetEventSlim(true);
69 68

  
70 69
        [System.ComponentModel.Composition.Import]
71 70
        public IStatusKeeper StatusKeeper { get; set; }
......
80 79
        public IPithosSettings Settings { get; set; }
81 80

  
82 81
        private bool _firstPoll = true;
82
        
83 83
        private TaskCompletionSource<bool> _tcs;
84
        private ConcurrentDictionary<string,DateTime> _lastSeen=new ConcurrentDictionary<string, DateTime>();
84
        private readonly AsyncManualResetEvent _pauseAgent = new AsyncManualResetEvent(true);
85

  
86
        private ConcurrentDictionary<string, DateTime> _lastSeen = new ConcurrentDictionary<string, DateTime>();
85 87

  
86 88
        public void Start()
87 89
        {
......
522 524

  
523 525
            using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push(accountInfo.UserName))
524 526
            {
525
                _pauseAgent.Wait();
527
                await _pauseAgent.WaitAsync();
526 528

  
527 529
                Log.Info("Scheduled");
528 530
                var client = new CloudFilesClient(accountInfo)
......
537 539

  
538 540
                try
539 541
                {
540
                    _pauseAgent.Wait();
542
                    await _pauseAgent.WaitAsync();
541 543
                    //Get the poll time now. We may miss some deletions but it's better to keep a file that was deleted
542 544
                    //than delete a file that was created while we were executing the poll                    
543 545
                    var pollTime = DateTime.Now;

Also available in: Unified diff