@@ -142,21 +142,25 @@ public static void DestroyWindow(string winName)
142142 if ( string . IsNullOrEmpty ( winName ) )
143143 throw new ArgumentException ( "null or empty string." , nameof ( winName ) ) ;
144144
145- NativeMethods . HandleException (
146- NativeMethods . highgui_destroyWindow ( winName ) ) ;
147-
148- ForgetWindowCallbacks ( winName ) ;
145+ lock ( highguiCallbackSync )
146+ {
147+ NativeMethods . HandleException (
148+ NativeMethods . highgui_destroyWindow ( winName ) ) ;
149+ ForgetWindowCallbacks ( winName ) ;
150+ }
149151 }
150152
151153 /// <summary>
152154 /// Destroys all of the HighGUI windows.
153155 /// </summary>
154156 public static void DestroyAllWindows ( )
155157 {
156- NativeMethods . HandleException (
157- NativeMethods . highgui_destroyAllWindows ( ) ) ;
158-
159- ForgetAllCallbacks ( ) ;
158+ lock ( highguiCallbackSync )
159+ {
160+ NativeMethods . HandleException (
161+ NativeMethods . highgui_destroyAllWindows ( ) ) ;
162+ ForgetAllCallbacks ( ) ;
163+ }
160164 }
161165
162166 /// <summary>
@@ -354,19 +358,22 @@ public static void SetMouseCallback(string windowName, MouseCallback onMouse, In
354358
355359 var context = new MouseCallbackContext { Callback = onMouse , UserData = userData } ;
356360 var contextHandle = GCHandle . Alloc ( context ) ;
357- try
358- {
359- NativeMethods . HandleException (
360- NativeMethods . highgui_setMouseCallback ( windowName , GetMouseCallbackTrampolinePointer ( ) , GCHandle . ToIntPtr ( contextHandle ) ) ) ;
361- }
362- catch
361+ lock ( highguiCallbackSync )
363362 {
364- contextHandle . Free ( ) ;
365- throw ;
366- }
363+ try
364+ {
365+ NativeMethods . HandleException (
366+ NativeMethods . highgui_setMouseCallback ( windowName , GetMouseCallbackTrampolinePointer ( ) , GCHandle . ToIntPtr ( contextHandle ) ) ) ;
367+ }
368+ catch
369+ {
370+ contextHandle . Free ( ) ;
371+ throw ;
372+ }
367373
368- // Root the context (and thus the delegate) for the lifetime of the window (see registry note above).
369- RegisterMouseCallback ( windowName , contextHandle ) ;
374+ // Root the context (and thus the delegate) for the lifetime of the window (see registry note above).
375+ RegisterMouseCallback ( windowName , contextHandle ) ;
376+ }
370377 }
371378
372379 /// <summary>
@@ -493,21 +500,24 @@ public static int CreateTrackbar(string trackbarName, string winName,
493500 }
494501
495502 int ret ;
496- try
497- {
498- NativeMethods . HandleException (
499- NativeMethods . highgui_createTrackbar (
500- trackbarName , winName , ref value , count , onChangePtr ,
501- contextHandle . HasValue ? GCHandle . ToIntPtr ( contextHandle . Value ) : IntPtr . Zero , out ret ) ) ;
502- }
503- catch
503+ lock ( highguiCallbackSync )
504504 {
505- contextHandle ? . Free ( ) ;
506- throw ;
507- }
505+ try
506+ {
507+ NativeMethods . HandleException (
508+ NativeMethods . highgui_createTrackbar (
509+ trackbarName , winName , ref value , count , onChangePtr ,
510+ contextHandle . HasValue ? GCHandle . ToIntPtr ( contextHandle . Value ) : IntPtr . Zero , out ret ) ) ;
511+ }
512+ catch
513+ {
514+ contextHandle ? . Free ( ) ;
515+ throw ;
516+ }
508517
509- // Root the context (and thus the delegate) for the lifetime of the window (see registry note above).
510- RegisterTrackbarCallback ( winName , trackbarName , contextHandle ) ;
518+ // Root the context (and thus the delegate) for the lifetime of the window (see registry note above).
519+ RegisterTrackbarCallback ( winName , trackbarName , contextHandle ) ;
520+ }
511521 return ret ;
512522 }
513523
@@ -542,21 +552,24 @@ public static int CreateTrackbar(string trackbarName, string winName,
542552 }
543553
544554 int ret ;
545- try
546- {
547- NativeMethods . HandleException (
548- NativeMethods . highgui_createTrackbar (
549- trackbarName , winName , IntPtr . Zero , count , onChangePtr ,
550- contextHandle . HasValue ? GCHandle . ToIntPtr ( contextHandle . Value ) : IntPtr . Zero , out ret ) ) ;
551- }
552- catch
555+ lock ( highguiCallbackSync )
553556 {
554- contextHandle ? . Free ( ) ;
555- throw ;
556- }
557+ try
558+ {
559+ NativeMethods . HandleException (
560+ NativeMethods . highgui_createTrackbar (
561+ trackbarName , winName , IntPtr . Zero , count , onChangePtr ,
562+ contextHandle . HasValue ? GCHandle . ToIntPtr ( contextHandle . Value ) : IntPtr . Zero , out ret ) ) ;
563+ }
564+ catch
565+ {
566+ contextHandle ? . Free ( ) ;
567+ throw ;
568+ }
557569
558- // Root the context (and thus the delegate) for the lifetime of the window (see registry note above).
559- RegisterTrackbarCallback ( winName , trackbarName , contextHandle ) ;
570+ // Root the context (and thus the delegate) for the lifetime of the window (see registry note above).
571+ RegisterTrackbarCallback ( winName , trackbarName , contextHandle ) ;
572+ }
560573 return ret ;
561574 }
562575
0 commit comments