File tree Expand file tree Collapse file tree 1 file changed +28
-15
lines changed Expand file tree Collapse file tree 1 file changed +28
-15
lines changed Original file line number Diff line number Diff line change @@ -155,30 +155,43 @@ private async Task AddScreenshot(bool @internal)
155
155
156
156
}
157
157
158
- internal async Task < Stream > GetScreenshotFromObs ( )
158
+ internal Task < Stream > GetScreenshotFromObs ( )
159
159
{
160
+ var source = new TaskCompletionSource < Stream > ( ) ;
160
161
161
- Stream result = null ;
162
+ var cancellationSource = new CancellationTokenSource ( 100 * 100 ) ;
163
+ cancellationSource . Token . Register ( ( ) => source . TrySetCanceled ( ) ) ;
162
164
163
165
ScreenshotSink . Instance . ScreenshotReceived += ( obj , args ) =>
164
166
{
165
- result = args . Screenshot ;
167
+ source . TrySetResult ( args . Screenshot ) ;
166
168
} ;
167
169
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 ) =>
169
173
{
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
+ } ) ;
181
193
194
+ return source . Task ;
182
195
}
183
196
184
197
public Task AddScreenshot ( )
You can’t perform that action at this time.
0 commit comments