File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -201,30 +201,43 @@ private async Task AddScreenshot(bool @internal)
201201
202202 }
203203
204- internal async Task < Stream > GetScreenshotFromObs ( )
204+ internal Task < Stream > GetScreenshotFromObs ( )
205205 {
206+ var source = new TaskCompletionSource < Stream > ( ) ;
206207
207- Stream result = null ;
208+ var cancellationSource = new CancellationTokenSource ( 100 * 100 ) ;
209+ cancellationSource . Token . Register ( ( ) => source . TrySetCanceled ( ) ) ;
208210
209211 ScreenshotSink . Instance . ScreenshotReceived += ( obj , args ) =>
210212 {
211- result = args . Screenshot ;
213+ source . TrySetResult ( args . Screenshot ) ;
212214 } ;
213215
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 ) =>
215219 {
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+ }
225227
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+ } ) ;
227239
240+ return source . Task ;
228241 }
229242
230243 public Task AddScreenshot ( )
You can’t perform that action at this time.
0 commit comments