Skip to content

Commit 8250546

Browse files
authored
Merge pull request #157 from wwwMADwww/HandlersFix156
Fix #156
2 parents db80989 + ab7a7a1 commit 8250546

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src_csharp/MonitorCaptureConfiguration.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public class MonitorCaptureConfiguration : IDisposable
2121
private bool disposedValue = false;
2222
private static int MonitorSizeHint = 8;
2323

24+
private static MonitorWindowCallbackWithContext _onCaptureWithContext = OnCapture;
25+
private static ScreenCaptureCallbackWithContext _onNewFrameWithContext = OnNewFrame;
26+
private static ScreenCaptureCallbackWithContext _onFrameChangedWithContext = OnFrameChanged;
27+
private static MouseCaptureCallbackWithContext _onMouseChangedWithContext = OnMouseChanged;
28+
2429
private static readonly UnmanagedHandles<MonitorCaptureConfiguration> UnmanagedHandles = new();
2530

2631
public static Monitor[] GetMonitors()
@@ -72,7 +77,7 @@ public MonitorCaptureConfiguration(MonitorCallback callback)
7277
{
7378
_monitorCallback = callback;
7479
UnmanagedHandles.Add(this, out _handle);
75-
Config = NativeFunctions.SCL_CreateMonitorCaptureConfigurationWithContext(OnCapture, _handle);
80+
Config = NativeFunctions.SCL_CreateMonitorCaptureConfigurationWithContext(_onCaptureWithContext, _handle);
7681
}
7782
catch
7883
{
@@ -87,7 +92,7 @@ public MonitorCaptureConfiguration OnNewFrame(Action<Image, Monitor> onNewFrame)
8792
if (_onNewFrame == null)
8893
{
8994
_onNewFrame = onNewFrame;
90-
NativeFunctions.SCL_MonitorOnNewFrameWithContext(Config, OnNewFrame);
95+
NativeFunctions.SCL_MonitorOnNewFrameWithContext(Config, _onNewFrameWithContext);
9196
}
9297
else
9398
{
@@ -104,7 +109,7 @@ public MonitorCaptureConfiguration OnFrameChanged(Action<Image, Monitor> onFrame
104109
if (_onFrameChanged == null)
105110
{
106111
_onFrameChanged = onFrameChanged;
107-
NativeFunctions.SCL_MonitorOnFrameChangedWithContext(Config, OnFrameChanged);
112+
NativeFunctions.SCL_MonitorOnFrameChangedWithContext(Config, _onFrameChangedWithContext);
108113
}
109114
else
110115
{
@@ -121,7 +126,7 @@ public MonitorCaptureConfiguration OnMouseChanged(Action<Image, MousePoint> onMo
121126
if (_onMouseChanged == null)
122127
{
123128
_onMouseChanged = onMouseChanged;
124-
NativeFunctions.SCL_MonitorOnMouseChangedWithContext(Config, OnMouseChanged);
129+
NativeFunctions.SCL_MonitorOnMouseChangedWithContext(Config, _onMouseChangedWithContext);
125130
}
126131
else
127132
{

src_csharp/WindowCaptureConfiguration.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public class WindowCaptureConfiguration : IDisposable
2626

2727
private static readonly UnmanagedHandles<WindowCaptureConfiguration> UnmanagedHandles = new();
2828

29+
private static MonitorWindowCallbackWithContext _onCaptureWithContext = OnCapture;
30+
private static WindowCaptureCallbackWithContext _onNewFrameWithContext = OnNewFrame;
31+
private static WindowCaptureCallbackWithContext _onFrameChangedWithContext = OnFrameChanged;
32+
private static MouseCaptureCallbackWithContext _onMouseChangedWithContext = OnMouseChanged;
33+
2934
public static Window[] GetWindows()
3035
{
3136
return Utility.CopyUnmanagedWithHint<Window>(ref WindowSizeHint, NativeFunctions.SCL_GetWindows);
@@ -73,7 +78,7 @@ public WindowCaptureConfiguration(WindowCallback callback)
7378
{
7479
_windowCallback = callback;
7580
UnmanagedHandles.Add(this, out _handle);
76-
Config = NativeFunctions.SCL_CreateWindowCaptureConfigurationWithContext(OnCapture, _handle);
81+
Config = NativeFunctions.SCL_CreateWindowCaptureConfigurationWithContext(_onCaptureWithContext, _handle);
7782
}
7883
catch
7984
{
@@ -88,7 +93,7 @@ public WindowCaptureConfiguration OnNewFrame(Action<Image, Window> onNewFrame)
8893
if (_onNewFrame == null)
8994
{
9095
_onNewFrame = onNewFrame;
91-
NativeFunctions.SCL_WindowOnNewFrameWithContext(Config, OnNewFrame);
96+
NativeFunctions.SCL_WindowOnNewFrameWithContext(Config, _onNewFrameWithContext);
9297
}
9398
else
9499
{
@@ -105,7 +110,7 @@ public WindowCaptureConfiguration OnFrameChanged(Action<Image, Window> onFrameCh
105110
if (_onFrameChanged == null)
106111
{
107112
_onFrameChanged = onFrameChanged;
108-
NativeFunctions.SCL_WindowOnFrameChangedWithContext(Config, OnFrameChanged);
113+
NativeFunctions.SCL_WindowOnFrameChangedWithContext(Config, _onFrameChangedWithContext);
109114
}
110115
else
111116
{
@@ -122,7 +127,7 @@ public WindowCaptureConfiguration OnMouseChanged(Action<Image, MousePoint> onMou
122127
if (_onMouseChanged == null)
123128
{
124129
_onMouseChanged = onMouseChanged;
125-
NativeFunctions.SCL_WindowOnMouseChangedWithContext(Config, OnMouseChanged);
130+
NativeFunctions.SCL_WindowOnMouseChangedWithContext(Config, _onMouseChangedWithContext);
126131
}
127132
else
128133
{

0 commit comments

Comments
 (0)