From a37e729d81a0154edc1d19d9d5faa04d92e05f76 Mon Sep 17 00:00:00 2001 From: Mark Junker Date: Fri, 16 Aug 2019 16:40:19 +0200 Subject: [PATCH] Wait for the task to be running. Fixes #75 Fixes #76 --- .../Networking/PausableFtpService.cs | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/FubarDev.FtpServer/Networking/PausableFtpService.cs b/src/FubarDev.FtpServer/Networking/PausableFtpService.cs index 527ba5a9..f28f8bd6 100644 --- a/src/FubarDev.FtpServer/Networking/PausableFtpService.cs +++ b/src/FubarDev.FtpServer/Networking/PausableFtpService.cs @@ -55,17 +55,31 @@ protected PausableFtpService( protected bool IsPauseRequested => _jobPaused.IsCancellationRequested; /// - public Task StartAsync(CancellationToken cancellationToken) + public async Task StartAsync(CancellationToken cancellationToken) { if (Status != FtpServiceStatus.ReadyToRun) { throw new InvalidOperationException($"Status must be {FtpServiceStatus.ReadyToRun}, but was {Status}."); } - _jobPaused = new CancellationTokenSource(); - _task = RunAsync(new Progress(status => Status = status)); - - return Task.CompletedTask; + using (var semaphore = new SemaphoreSlim(0, 1)) + { + _jobPaused = new CancellationTokenSource(); + _task = RunAsync( + new Progress( + status => + { + Status = status; + + if (status == FtpServiceStatus.Running) + { + // ReSharper disable once AccessToDisposedClosure + semaphore.Release(); + } + })); + + await semaphore.WaitAsync(cancellationToken); + } } /// @@ -140,7 +154,21 @@ public async Task ContinueAsync(CancellationToken cancellationToken) await OnContinueRequestingAsync(cancellationToken) .ConfigureAwait(false); - _task = RunAsync(new Progress(status => Status = status)); + using (var semaphore = new SemaphoreSlim(0, 1)) + { + _task = RunAsync(new Progress(status => + { + Status = status; + + if (status == FtpServiceStatus.Running) + { + // ReSharper disable once AccessToDisposedClosure + semaphore.Release(); + } + })); + + await semaphore.WaitAsync(cancellationToken); + } } [NotNull]