|
|
@@ -3,6 +3,8 @@ using System.Runtime.InteropServices;
|
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
|
using Avalonia.Platform.Interop;
|
|
|
+using JetBrains.Annotations;
|
|
|
+
|
|
|
// ReSharper disable IdentifierTypo
|
|
|
namespace Avalonia.X11.NativeDialogs
|
|
|
{
|
|
|
@@ -256,10 +258,19 @@ namespace Avalonia.X11.NativeDialogs
|
|
|
|
|
|
public static IntPtr GetForeignWindow(IntPtr xid) => gdk_x11_window_foreign_new_for_display(s_display, xid);
|
|
|
|
|
|
+ static object s_startGtkLock = new();
|
|
|
+ static Task<bool> s_startGtkTask;
|
|
|
+
|
|
|
public static Task<bool> StartGtk()
|
|
|
{
|
|
|
- var tcs = new TaskCompletionSource<bool>();
|
|
|
- new Thread(() =>
|
|
|
+ return StartGtkCore();
|
|
|
+ lock (s_startGtkLock)
|
|
|
+ return s_startGtkTask ??= StartGtkCore();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void GtkThread(TaskCompletionSource<bool> tcs)
|
|
|
+ {
|
|
|
+ try
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
@@ -293,7 +304,17 @@ namespace Avalonia.X11.NativeDialogs
|
|
|
tcs.SetResult(true);
|
|
|
while (true)
|
|
|
gtk_main_iteration();
|
|
|
- }) {Name = "GTK3THREAD", IsBackground = true}.Start();
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ tcs.SetResult(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Task<bool> StartGtkCore()
|
|
|
+ {
|
|
|
+ var tcs = new TaskCompletionSource<bool>();
|
|
|
+ new Thread(() => GtkThread(tcs)) {Name = "GTK3THREAD", IsBackground = true}.Start();
|
|
|
return tcs.Task;
|
|
|
}
|
|
|
}
|