Skip to content

Commit

Permalink
Wait for the task to be running.
Browse files Browse the repository at this point in the history
Fixes #75
Fixes #76
  • Loading branch information
fubar-coder committed Aug 16, 2019
1 parent f4fc7ba commit a37e729
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/FubarDev.FtpServer/Networking/PausableFtpService.cs
Expand Up @@ -55,17 +55,31 @@ internal abstract class PausableFtpService : IPausableFtpService
protected bool IsPauseRequested => _jobPaused.IsCancellationRequested;

/// <inheritdoc />
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<FtpServiceStatus>(status => Status = status));

return Task.CompletedTask;
using (var semaphore = new SemaphoreSlim(0, 1))
{
_jobPaused = new CancellationTokenSource();
_task = RunAsync(
new Progress<FtpServiceStatus>(
status =>
{
Status = status;
if (status == FtpServiceStatus.Running)
{
// ReSharper disable once AccessToDisposedClosure
semaphore.Release();
}
}));

await semaphore.WaitAsync(cancellationToken);
}
}

/// <inheritdoc />
Expand Down Expand Up @@ -140,7 +154,21 @@ public async Task ContinueAsync(CancellationToken cancellationToken)
await OnContinueRequestingAsync(cancellationToken)
.ConfigureAwait(false);

_task = RunAsync(new Progress<FtpServiceStatus>(status => Status = status));
using (var semaphore = new SemaphoreSlim(0, 1))
{
_task = RunAsync(new Progress<FtpServiceStatus>(status =>
{
Status = status;
if (status == FtpServiceStatus.Running)
{
// ReSharper disable once AccessToDisposedClosure
semaphore.Release();
}
}));

await semaphore.WaitAsync(cancellationToken);
}
}

[NotNull]
Expand Down

0 comments on commit a37e729

Please sign in to comment.