This repository was archived by the owner on Oct 4, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +8
-8
lines changed
main/src/core/MonoDevelop.Core/MonoDevelop.Core Expand file tree Collapse file tree 1 file changed +8
-8
lines changed Original file line number Diff line number Diff line change @@ -330,15 +330,15 @@ public static SynchronizationContext MainSynchronizationContext {
330
330
/// </summary>
331
331
public static Task RunInMainThread ( Action action )
332
332
{
333
- var ts = new TaskCompletionSource < int > ( ) ;
334
333
if ( IsMainThread ) {
335
334
try {
336
335
action ( ) ;
337
- ts . SetResult ( 0 ) ;
336
+ return Task . CompletedTask ;
338
337
} catch ( Exception ex ) {
339
- ts . SetException ( ex ) ;
338
+ return Task . FromException ( ex ) ;
340
339
}
341
340
} else {
341
+ var ts = new TaskCompletionSource < int > ( ) ;
342
342
MainSynchronizationContext . Post ( state => {
343
343
var ( act , tcs ) = ( ValueTuple < Action , TaskCompletionSource < int > > ) state ;
344
344
try {
@@ -348,23 +348,23 @@ public static Task RunInMainThread (Action action)
348
348
tcs . SetException ( ex ) ;
349
349
}
350
350
} , ( action , ts ) ) ;
351
+ return ts . Task ;
351
352
}
352
- return ts . Task ;
353
353
}
354
354
355
355
/// <summary>
356
356
/// Runs a function in the main thread (usually the UI thread). The method returns a task, so it can be awaited.
357
357
/// </summary>
358
358
public static Task < T > RunInMainThread < T > ( Func < T > func )
359
359
{
360
- var ts = new TaskCompletionSource < T > ( ) ;
361
360
if ( IsMainThread ) {
362
361
try {
363
- ts . SetResult ( func ( ) ) ;
362
+ return Task . FromResult ( func ( ) ) ;
364
363
} catch ( Exception ex ) {
365
- ts . SetException ( ex ) ;
364
+ return Task . FromException < T > ( ex ) ;
366
365
}
367
366
} else {
367
+ var ts = new TaskCompletionSource < T > ( ) ;
368
368
MainSynchronizationContext . Post ( state => {
369
369
var ( fun , tcs ) = ( ValueTuple < Func < T > , TaskCompletionSource < T > > ) state ;
370
370
try {
@@ -373,8 +373,8 @@ public static Task<T> RunInMainThread<T> (Func<T> func)
373
373
tcs . SetException ( ex ) ;
374
374
}
375
375
} , ( func , ts ) ) ;
376
+ return ts . Task ;
376
377
}
377
- return ts . Task ;
378
378
}
379
379
380
380
/// <summary>
You can’t perform that action at this time.
0 commit comments