[Mac]] Add NSApplication.IsUIThread property

This can be used to check whether it is safe to access UI components in
the calling thread.
This commit is contained in:
thefiddler 2014-07-23 21:14:47 +02:00
parent 050a93d4af
commit e5e9ae0929

View file

@ -29,7 +29,9 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading;
using OpenTK.Platform.MacOS; using OpenTK.Platform.MacOS;
namespace OpenTK.Platform.MacOS namespace OpenTK.Platform.MacOS
@ -41,6 +43,9 @@ namespace OpenTK.Platform.MacOS
static readonly IntPtr selQuit = Selector.Get("quit"); static readonly IntPtr selQuit = Selector.Get("quit");
static readonly int ThreadId =
System.Threading.Thread.CurrentThread.ManagedThreadId;
internal static void Initialize() { } internal static void Initialize() { }
static NSApplication() static NSApplication()
@ -103,9 +108,26 @@ namespace OpenTK.Platform.MacOS
Cocoa.SendVoid(settings, Selector.Release); 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<CancelEventArgs> Quit = delegate { }; internal static event EventHandler<CancelEventArgs> Quit = delegate { };
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
delegate void OnQuitDelegate(IntPtr self, IntPtr cmd); delegate void OnQuitDelegate(IntPtr self, IntPtr cmd);
static OnQuitDelegate OnQuitHandler = OnQuit; static OnQuitDelegate OnQuitHandler = OnQuit;
static void OnQuit(IntPtr self, IntPtr cmd) static void OnQuit(IntPtr self, IntPtr cmd)
{ {