Ejemplo n.º 1
0
 public IAsyncResult BeginAcceptFile(AsyncCallback callback, object state)
 {
     if (!Active)
         throw new InvalidOperationException("watch_stopped");
     // async
     var asyncResult = new AcceptAsyncResult(this, state, callback);
     WaitResult(asyncResult);
     return asyncResult;
 }
Ejemplo n.º 2
0
 public void DoBeginAccept(AcceptAsyncResult asyncResult)
 {
     if (!Active)
         throw new InvalidOperationException("mustlisten");
     var processed = false;
     var watcher = this;
     var success = FileWatcherError.Success;
     var acceptQueue = GetAcceptQueue();
     var lockTaken = false;
     try
     {
         Monitor.Enter(watcher, ref lockTaken);
         if (acceptQueue.Count == 0)
         {
             object result = null;
             try { success = DoWaitResult(out result); }
             catch (ObjectDisposedException) { success = FileWatcherError.OperationAborted; }
             switch (success)
             {
                 case FileWatcherError.WouldBlock:
                     acceptQueue.Enqueue(asyncResult);
                     if (!SetAsyncEventSelect())
                     {
                         acceptQueue.Dequeue();
                         throw new ObjectDisposedException(base.GetType().FullName);
                     }
                     goto end;
                 case FileWatcherError.Success:
                     asyncResult.Result = result;
                     break;
                 default:
                     asyncResult.ErrorCode = (int)success;
                     break;
             }
             processed = true;
         }
         else
             acceptQueue.Enqueue(asyncResult);
     }
     finally { if (lockTaken) Monitor.Exit(watcher); }
     end:
     if (!processed)
         return;
     if (success == FileWatcherError.Success)
         asyncResult.InvokeCallback();
     else
     {
         var ex = new FileWatcherException(success);
         if (_logger != null) _logger.Debug("Exception:DoBeginAccept", ex);
         throw ex;
     }
 }
Ejemplo n.º 3
0
 public IAsyncResult BeginAcceptFile(AsyncCallback callback, object state)
 {
     if (!Active)
         throw new InvalidOperationException("watch_stopped");
     // async
     if (_logger != null) _logger.DebugFormat("Enter:BeginAcceptFile");
     if (CleanedUp)
         throw new ObjectDisposedException(base.GetType().FullName);
     var asyncResult = new AcceptAsyncResult(this, state, callback);
     asyncResult.StartAsync();
     DoBeginAccept(asyncResult);
     asyncResult.FinishAsync();
     if (_logger != null) _logger.InfoFormat("Exit:BeginAcceptFile {0}", asyncResult);
     return asyncResult;
 }
Ejemplo n.º 4
0
 private void WaitResultCallback(AcceptAsyncResult asyncResult)
 {
     if (!Active)
         throw new InvalidOperationException("mustlisten");
     using (var tx = new TransactionScope(TransactionScopeOption.Suppress))
         foreach (var r in _tcs.Task.Result)
             switch (r.Error)
             {
                 case FileWatcherError.Success:
                     asyncResult.Result = r;
                     asyncResult.InvokeCallback();
                     break;
                 default:
                     //asyncResult.ErrorCode = (int)r.Error;
                     throw new FileWatcherException(r.Error);
             }
     //var success = FileWatcherError.Success;
     //IEnumerable<FileWatcherFile> result;
     //lock (this)
     //{
     //result = _tcs.Task.Result;
     //success = result.Error;
     //switch (success)
     //{
     //    case FileWatcherError.Success:
     //        asyncResult.Result = result;
     //        break;
     //    default:
     //        asyncResult.ErrorCode = (int)success;
     //        break;
     //}
     //}
 }
Ejemplo n.º 5
0
 private void WaitResult(AcceptAsyncResult asyncResult)
 {
     Task.Factory.StartNew(s => WaitResultCallback((AcceptAsyncResult)s), asyncResult, CancellationToken.None, TaskCreationOptions.PreferFairness, TaskScheduler.Default);
 }