Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
EventWaitHandle.cs
Go to the documentation of this file.
2using System.IO;
6
7namespace System.Threading;
8
10{
11 public EventWaitHandle(bool initialState, EventResetMode mode)
12 : this(initialState, mode, null, out var _)
13 {
14 }
15
16 public EventWaitHandle(bool initialState, EventResetMode mode, string? name)
17 : this(initialState, mode, name, out var _)
18 {
19 }
20
21 public EventWaitHandle(bool initialState, EventResetMode mode, string? name, out bool createdNew)
22 {
23 if (mode != 0 && mode != EventResetMode.ManualReset)
24 {
25 throw new ArgumentException(SR.Argument_InvalidFlag, "mode");
26 }
27 CreateEventCore(initialState, mode, name, out createdNew);
28 }
29
30 [SupportedOSPlatform("windows")]
31 public static EventWaitHandle OpenExisting(string name)
32 {
33 EventWaitHandle result;
34 return OpenExistingWorker(name, out result) switch
35 {
36 OpenExistingResult.NameNotFound => throw new WaitHandleCannotBeOpenedException(),
38 OpenExistingResult.PathNotFound => throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, name)),
39 _ => result,
40 };
41 }
42
43 [SupportedOSPlatform("windows")]
44 public static bool TryOpenExisting(string name, [NotNullWhen(true)] out EventWaitHandle? result)
45 {
46 return OpenExistingWorker(name, out result) == OpenExistingResult.Success;
47 }
48
50 {
51 base.SafeWaitHandle = handle;
52 }
53
54 private void CreateEventCore(bool initialState, EventResetMode mode, string name, out bool createdNew)
55 {
56 uint num = (initialState ? 2u : 0u);
57 if (mode == EventResetMode.ManualReset)
58 {
59 num |= 1u;
60 }
61 SafeWaitHandle safeWaitHandle = Interop.Kernel32.CreateEventEx(IntPtr.Zero, name, num, 34603010u);
62 int lastPInvokeError = Marshal.GetLastPInvokeError();
63 if (safeWaitHandle.IsInvalid)
64 {
65 safeWaitHandle.SetHandleAsInvalid();
66 if (!string.IsNullOrEmpty(name) && lastPInvokeError == 6)
67 {
69 }
70 throw Win32Marshal.GetExceptionForWin32Error(lastPInvokeError, name);
71 }
72 createdNew = lastPInvokeError != 183;
73 base.SafeWaitHandle = safeWaitHandle;
74 }
75
76 private static OpenExistingResult OpenExistingWorker(string name, out EventWaitHandle result)
77 {
78 if (name == null)
79 {
80 throw new ArgumentNullException("name");
81 }
82 if (name.Length == 0)
83 {
84 throw new ArgumentException(SR.Argument_EmptyName, "name");
85 }
86 result = null;
87 SafeWaitHandle safeWaitHandle = Interop.Kernel32.OpenEvent(34603010u, inheritHandle: false, name);
88 if (safeWaitHandle.IsInvalid)
89 {
90 int lastPInvokeError = Marshal.GetLastPInvokeError();
91 switch (lastPInvokeError)
92 {
93 case 2:
94 case 123:
95 return OpenExistingResult.NameNotFound;
96 case 3:
97 return OpenExistingResult.PathNotFound;
98 case 6:
99 return OpenExistingResult.NameInvalid;
100 default:
101 throw Win32Marshal.GetExceptionForWin32Error(lastPInvokeError, name);
102 }
103 }
104 result = new EventWaitHandle(safeWaitHandle);
105 return OpenExistingResult.Success;
106 }
107
108 public bool Reset()
109 {
110 bool flag = Interop.Kernel32.ResetEvent(base.SafeWaitHandle);
111 if (!flag)
112 {
114 }
115 return flag;
116 }
117
118 public bool Set()
119 {
120 bool flag = Interop.Kernel32.SetEvent(base.SafeWaitHandle);
121 if (!flag)
122 {
124 }
125 return flag;
126 }
127
128 internal static bool Set(SafeWaitHandle waitHandle)
129 {
130 return Interop.Kernel32.SetEvent(waitHandle);
131 }
132}
static bool ResetEvent(SafeWaitHandle handle)
static SafeWaitHandle OpenEvent(uint desiredAccess, bool inheritHandle, string name)
static bool SetEvent(SafeWaitHandle handle)
static SafeWaitHandle CreateEventEx(IntPtr lpSecurityAttributes, string name, uint flags, uint desiredAccess)
static Exception GetExceptionForLastWin32Error(string path="")
static Exception GetExceptionForWin32Error(int errorCode, string path="")
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Argument_InvalidFlag
Definition SR.cs:688
static string Threading_WaitHandleCannotBeOpenedException_InvalidHandle
Definition SR.cs:1996
static string Argument_EmptyName
Definition SR.cs:584
static string IO_PathNotFound_Path
Definition SR.cs:44
Definition SR.cs:7
static bool TryOpenExisting(string name, [NotNullWhen(true)] out EventWaitHandle? result)
static EventWaitHandle OpenExisting(string name)
static bool Set(SafeWaitHandle waitHandle)
EventWaitHandle(bool initialState, EventResetMode mode, string? name)
EventWaitHandle(bool initialState, EventResetMode mode)
EventWaitHandle(SafeWaitHandle handle)
static OpenExistingResult OpenExistingWorker(string name, out EventWaitHandle result)
EventWaitHandle(bool initialState, EventResetMode mode, string? name, out bool createdNew)
void CreateEventCore(bool initialState, EventResetMode mode, string name, out bool createdNew)
static readonly IntPtr Zero
Definition IntPtr.cs:18