File tree Expand file tree Collapse file tree 1 file changed +27
-14
lines changed Expand file tree Collapse file tree 1 file changed +27
-14
lines changed Original file line number Diff line number Diff line change @@ -201,30 +201,43 @@ private async Task AddScreenshot(bool @internal)
201
201
202
202
}
203
203
204
- internal async Task < Stream > GetScreenshotFromObs ( )
204
+ internal Task < Stream > GetScreenshotFromObs ( )
205
205
{
206
+ var source = new TaskCompletionSource < Stream > ( ) ;
206
207
207
- Stream result = null ;
208
+ var cancellationSource = new CancellationTokenSource ( 100 * 100 ) ;
209
+ cancellationSource . Token . Register ( ( ) => source . TrySetCanceled ( ) ) ;
208
210
209
211
ScreenshotSink . Instance . ScreenshotReceived += ( obj , args ) =>
210
212
{
211
- result = args . Screenshot ;
213
+ source . TrySetResult ( args . Screenshot ) ;
212
214
} ;
213
215
214
- using ( var scope = _Services . CreateScope ( ) )
216
+ var scope = _Services . CreateScope ( ) ;
217
+ var obsContext = scope . ServiceProvider . GetRequiredService < IHubContext < ObsHub , ITakeScreenshots > > ( ) ;
218
+ _ = obsContext . Clients . All . TakeScreenshot ( ) . ContinueWith ( ( t ) =>
215
219
{
216
- var obsContext = scope . ServiceProvider . GetRequiredService < IHubContext < ObsHub , ITakeScreenshots > > ( ) ;
217
- await obsContext . Clients . All . TakeScreenshot ( ) ;
218
- }
219
- var i = 0 ;
220
- while ( result == null ) {
221
- await Task . Delay ( 100 ) ;
222
- i ++ ;
223
- if ( i >= 100 ) break ;
224
- }
220
+ try
221
+ {
222
+ if ( t . IsFaulted )
223
+ {
224
+ source . TrySetException ( t . Exception . InnerExceptions ) ;
225
+ return ;
226
+ }
225
227
226
- return result ;
228
+ if ( t . IsCanceled || cancellationSource . IsCancellationRequested )
229
+ {
230
+ source . TrySetCanceled ( ) ;
231
+ return ;
232
+ }
233
+ }
234
+ finally
235
+ {
236
+ scope . Dispose ( ) ;
237
+ }
238
+ } ) ;
227
239
240
+ return source . Task ;
228
241
}
229
242
230
243
public Task AddScreenshot ( )
You can’t perform that action at this time.
0 commit comments