File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -155,30 +155,43 @@ private async Task AddScreenshot(bool @internal)
155155
156156 }
157157
158- internal async Task < Stream > GetScreenshotFromObs ( )
158+ internal Task < Stream > GetScreenshotFromObs ( )
159159 {
160+ var source = new TaskCompletionSource < Stream > ( ) ;
160161
161- Stream result = null ;
162+ var cancellationSource = new CancellationTokenSource ( 100 * 100 ) ;
163+ cancellationSource . Token . Register ( ( ) => source . TrySetCanceled ( ) ) ;
162164
163165 ScreenshotSink . Instance . ScreenshotReceived += ( obj , args ) =>
164166 {
165- result = args . Screenshot ;
167+ source . TrySetResult ( args . Screenshot ) ;
166168 } ;
167169
168- using ( var scope = _Services . CreateScope ( ) )
170+ var scope = _Services . CreateScope ( ) ;
171+ var obsContext = scope . ServiceProvider . GetRequiredService < IHubContext < ObsHub , ITakeScreenshots > > ( ) ;
172+ _ = obsContext . Clients . All . TakeScreenshot ( ) . ContinueWith ( ( t ) =>
169173 {
170- var obsContext = scope . ServiceProvider . GetRequiredService < IHubContext < ObsHub , ITakeScreenshots > > ( ) ;
171- await obsContext . Clients . All . TakeScreenshot ( ) ;
172- }
173- var i = 0 ;
174- while ( result == null ) {
175- await Task . Delay ( 100 ) ;
176- i ++ ;
177- if ( i >= 100 ) break ;
178- }
179-
180- return result ;
174+ try
175+ {
176+ if ( t . IsFaulted )
177+ {
178+ source . TrySetException ( t . Exception . InnerExceptions ) ;
179+ return ;
180+ }
181+
182+ if ( t . IsCanceled || cancellationSource . IsCancellationRequested )
183+ {
184+ source . TrySetCanceled ( ) ;
185+ return ;
186+ }
187+ }
188+ finally
189+ {
190+ scope . Dispose ( ) ;
191+ }
192+ } ) ;
181193
194+ return source . Task ;
182195 }
183196
184197 public Task AddScreenshot ( )
You can’t perform that action at this time.
0 commit comments