@@ -55,17 +55,31 @@ protected PausableFtpService(
5555 protected bool IsPauseRequested => _jobPaused . IsCancellationRequested ;
5656
5757 /// <inheritdoc />
58- public Task StartAsync ( CancellationToken cancellationToken )
58+ public async Task StartAsync ( CancellationToken cancellationToken )
5959 {
6060 if ( Status != FtpServiceStatus . ReadyToRun )
6161 {
6262 throw new InvalidOperationException ( $ "Status must be { FtpServiceStatus . ReadyToRun } , but was { Status } .") ;
6363 }
6464
65- _jobPaused = new CancellationTokenSource ( ) ;
66- _task = RunAsync ( new Progress < FtpServiceStatus > ( status => Status = status ) ) ;
67-
68- return Task . CompletedTask ;
65+ using ( var semaphore = new SemaphoreSlim ( 0 , 1 ) )
66+ {
67+ _jobPaused = new CancellationTokenSource ( ) ;
68+ _task = RunAsync (
69+ new Progress < FtpServiceStatus > (
70+ status =>
71+ {
72+ Status = status ;
73+
74+ if ( status == FtpServiceStatus . Running )
75+ {
76+ // ReSharper disable once AccessToDisposedClosure
77+ semaphore . Release ( ) ;
78+ }
79+ } ) ) ;
80+
81+ await semaphore . WaitAsync ( cancellationToken ) ;
82+ }
6983 }
7084
7185 /// <inheritdoc />
@@ -140,7 +154,21 @@ public async Task ContinueAsync(CancellationToken cancellationToken)
140154 await OnContinueRequestingAsync ( cancellationToken )
141155 . ConfigureAwait ( false ) ;
142156
143- _task = RunAsync ( new Progress < FtpServiceStatus > ( status => Status = status ) ) ;
157+ using ( var semaphore = new SemaphoreSlim ( 0 , 1 ) )
158+ {
159+ _task = RunAsync ( new Progress < FtpServiceStatus > ( status =>
160+ {
161+ Status = status ;
162+
163+ if ( status == FtpServiceStatus . Running )
164+ {
165+ // ReSharper disable once AccessToDisposedClosure
166+ semaphore . Release ( ) ;
167+ }
168+ } ) ) ;
169+
170+ await semaphore . WaitAsync ( cancellationToken ) ;
171+ }
144172 }
145173
146174 [ NotNull ]
0 commit comments