-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathThreadExtensions.cs
More file actions
76 lines (72 loc) · 3.02 KB
/
ThreadExtensions.cs
File metadata and controls
76 lines (72 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Diagnostics;
using System.Runtime.Versioning;
namespace BytecodeApi.Extensions;
/// <summary>
/// Provides a set of <see langword="static" /> methods for interaction with <see cref="Thread" /> objects.
/// </summary>
[SupportedOSPlatform("windows")]
public static class ThreadExtensions
{
extension(Thread)
{
/// <summary>
/// Creates a new background <see cref="Thread" /> with the STA apartment state and starts it. <see cref="ThreadAbortException" /> exceptions are swallowed.
/// </summary>
/// <param name="action">The <see cref="Action" /> to be invoked from the new <see cref="Thread" />.</param>
/// <returns>
/// The <see cref="Thread" /> this method creates.
/// </returns>
public static Thread StartNew(Action action)
{
return StartNew(action, null);
}
/// <summary>
/// Creates a new background <see cref="Thread" /> with the STA apartment state and starts it. <see cref="ThreadAbortException" /> exceptions are swallowed.
/// </summary>
/// <param name="action">The <see cref="Action" /> to be invoked from the new <see cref="Thread" />.</param>
/// <param name="exceptionHandler">An <see cref="Action{T}" /> that is called by the exception handler. If <see langword="null" />, the exception is rethrown. Use <see langword="delegate" /> { } to swallow exceptions. The stack trace prior to thread creation will be appended.</param>
/// <returns>
/// The <see cref="Thread" /> this method creates.
/// </returns>
public static Thread StartNew(Action action, Action<Exception>? exceptionHandler)
{
return StartNew(action, exceptionHandler, ThreadPriority.Normal);
}
/// <summary>
/// Creates a new background <see cref="Thread" /> with the STA apartment state and starts it. <see cref="ThreadAbortException" /> exceptions are swallowed.
/// </summary>
/// <param name="action">The <see cref="Action" /> to be invoked from the new <see cref="Thread" />.</param>
/// <param name="exceptionHandler">An <see cref="Action{T}" /> that is called by the exception handler. If <see langword="null" />, the exception is rethrown. Use <see langword="delegate" /> { } to swallow exceptions. The stack trace prior to thread creation will be appended.</param>
/// <param name="priority">The <see cref="ThreadPriority" /> for the new <see cref="Thread" />.</param>
/// <returns>
/// The <see cref="Thread" /> this method creates.
/// </returns>
public static Thread StartNew(Action action, Action<Exception>? exceptionHandler, ThreadPriority priority)
{
Check.ArgumentNull(action);
StackTrace stackTrace = new();
Thread thread = new(() =>
{
try
{
action();
}
catch (ThreadAbortException)
{
}
catch (Exception ex) when (exceptionHandler != null)
{
ex.AppendStackTrace(stackTrace);
exceptionHandler(ex);
}
})
{
IsBackground = true,
Priority = priority
};
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
return thread;
}
}
}