-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
142 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Runtime.ExceptionServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Threading.Tasks.Schedulers; | ||
|
||
namespace IS4.SFI.ConsoleApp | ||
{ | ||
/// <summary> | ||
/// Used to execute code in a STA context. | ||
/// </summary> | ||
static class StaThread | ||
{ | ||
static readonly TaskScheduler scheduler = new StaTaskScheduler(1); | ||
|
||
static bool IsSTA => Thread.CurrentThread.GetApartmentState() == ApartmentState.STA; | ||
|
||
public static void Invoke(Action action) | ||
{ | ||
if(IsSTA) | ||
{ | ||
action(); | ||
}else{ | ||
try{ | ||
Task.Factory.StartNew(action, CancellationToken.None, 0, scheduler).Wait(); | ||
}catch(AggregateException e) when(e.InnerExceptions.Count == 1) | ||
{ | ||
ExceptionDispatchInfo.Capture(e.InnerException!).Throw(); | ||
throw; | ||
} | ||
} | ||
} | ||
|
||
public static T Invoke<T>(Func<T> func) | ||
{ | ||
if(IsSTA) | ||
{ | ||
return func(); | ||
}else{ | ||
try{ | ||
return Task<T>.Factory.StartNew(func, CancellationToken.None, 0, scheduler).Result; | ||
}catch(AggregateException e) when(e.InnerExceptions.Count == 1) | ||
{ | ||
ExceptionDispatchInfo.Capture(e.InnerException!).Throw(); | ||
throw; | ||
} | ||
} | ||
} | ||
|
||
public static ValueTask InvokeAsync(Action action) | ||
{ | ||
if(IsSTA) | ||
{ | ||
action(); | ||
return new(); | ||
}else{ | ||
return new(Task.Factory.StartNew(action, CancellationToken.None, 0, scheduler)); | ||
} | ||
} | ||
|
||
public static ValueTask<T> InvokeAsync<T>(Func<T> func) | ||
{ | ||
if(IsSTA) | ||
{ | ||
return new(func()); | ||
}else{ | ||
return new(Task<T>.Factory.StartNew(func, CancellationToken.None, 0, scheduler)); | ||
} | ||
} | ||
} | ||
} |