From e5e9ae0929dfe079be561ace03907cec31805955 Mon Sep 17 00:00:00 2001 From: thefiddler Date: Wed, 23 Jul 2014 21:14:47 +0200 Subject: [PATCH] [Mac]] Add NSApplication.IsUIThread property This can be used to check whether it is safe to access UI components in the calling thread. --- .../Platform/MacOS/Cocoa/NSApplication.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Source/OpenTK/Platform/MacOS/Cocoa/NSApplication.cs b/Source/OpenTK/Platform/MacOS/Cocoa/NSApplication.cs index 22295b07..bdab0212 100644 --- a/Source/OpenTK/Platform/MacOS/Cocoa/NSApplication.cs +++ b/Source/OpenTK/Platform/MacOS/Cocoa/NSApplication.cs @@ -29,7 +29,9 @@ using System; using System.ComponentModel; +using System.Diagnostics; using System.Runtime.InteropServices; +using System.Threading; using OpenTK.Platform.MacOS; namespace OpenTK.Platform.MacOS @@ -41,6 +43,9 @@ namespace OpenTK.Platform.MacOS static readonly IntPtr selQuit = Selector.Get("quit"); + static readonly int ThreadId = + System.Threading.Thread.CurrentThread.ManagedThreadId; + internal static void Initialize() { } static NSApplication() @@ -103,9 +108,26 @@ namespace OpenTK.Platform.MacOS Cocoa.SendVoid(settings, Selector.Release); } + internal static bool IsUIThread + { + get + { + int thread_id = Thread.CurrentThread.ManagedThreadId; + bool is_ui_thread = thread_id == NSApplication.ThreadId; + if (!is_ui_thread) + { + Debug.Print("[Warning] UI resources must be disposed in the UI thread #{0}, not #{1}.", + NSApplication.ThreadId, thread_id); + } + return is_ui_thread; + } + } + internal static event EventHandler Quit = delegate { }; + [UnmanagedFunctionPointer(CallingConvention.Winapi)] delegate void OnQuitDelegate(IntPtr self, IntPtr cmd); + static OnQuitDelegate OnQuitHandler = OnQuit; static void OnQuit(IntPtr self, IntPtr cmd) {