Skip to content

Commit d09d208

Browse files
committed
Added lock while open / close a process.
1 parent 2009da2 commit d09d208

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Memory/ProcessInfo.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace ReClassNET.Memory
66
{
77
public class ProcessInfo : IDisposable
88
{
9+
private readonly object sync = new object();
10+
911
private readonly NativeHelper nativeHelper;
1012

1113
private IntPtr handle;
@@ -37,7 +39,13 @@ public IntPtr Open()
3739
{
3840
if (handle.IsNull())
3941
{
40-
handle = nativeHelper.OpenRemoteProcess(Id, NativeMethods.PROCESS_ALL_ACCESS);
42+
lock (sync)
43+
{
44+
if (handle.IsNull())
45+
{
46+
handle = nativeHelper.OpenRemoteProcess(Id, NativeMethods.PROCESS_ALL_ACCESS);
47+
}
48+
}
4149
}
4250
return handle;
4351
}
@@ -46,9 +54,15 @@ public void Close()
4654
{
4755
if (!handle.IsNull())
4856
{
49-
nativeHelper.CloseRemoteProcess(handle);
57+
lock (sync)
58+
{
59+
if (!handle.IsNull())
60+
{
61+
nativeHelper.CloseRemoteProcess(handle);
5062

51-
handle = IntPtr.Zero;
63+
handle = IntPtr.Zero;
64+
}
65+
}
5266
}
5367
}
5468
}

0 commit comments

Comments
 (0)