Skip to content

Commit

Permalink
Try to fix editor hang by aborting cancel dialog threads
Browse files Browse the repository at this point in the history
  • Loading branch information
mtschoen-unity committed Aug 14, 2020
1 parent cda6d7e commit b1f7ac7
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Runtime/Scripts/Helpers/EditorWindowCapture.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

#if UNITY_EDITOR_WIN
using System.Runtime.InteropServices;
using System.Threading;
Expand Down Expand Up @@ -33,6 +35,10 @@ sealed class EditorWindowCapture : MonoBehaviour
MethodInfo m_GrabPixels;
Rect m_ScaledRect;

#if UNITY_EDITOR_WIN
readonly List<Thread> m_CancelDialogThreads = new List<Thread>();
#endif

/// <summary>
/// RenderTexture that represents the captured Editor Window
/// Updated frequently, when capture is enabled
Expand Down Expand Up @@ -99,6 +105,15 @@ void OnDisable()
{
if (m_Window)
m_Window.Close();

#if UNITY_EDITOR_WIN
foreach (var cancelDialogThread in m_CancelDialogThreads)
{
cancelDialogThread.Abort();
}

m_CancelDialogThreads.Clear();
#endif
}

void Update()
Expand Down Expand Up @@ -158,13 +173,17 @@ public void SendEvent(Transform rayOrigin, Transform workspace, EventType type)
// Thread is needed because context menu blocks main thread
if (type == EventType.MouseDown)
{
new Thread(() =>
var thread = new Thread(() =>
{
const int HWND_BROADCAST = 0xffff;
const int WM_CANCELMODE = 0x001F;
var hwnd = new IntPtr(HWND_BROADCAST);
SendMessage(hwnd, WM_CANCELMODE, 0, IntPtr.Zero);
}).Start();
});

m_CancelDialogThreads.Add(thread);

thread.Start();
}
#endif

Expand Down

0 comments on commit b1f7ac7

Please sign in to comment.