using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEditor;
namespace UnityTcp.Editor.Helpers
{
///
/// Helper class for main thread operations
///
public static class MainThreadHelper
{
private static int mainThreadId;
///
/// Initialize the main thread ID for safe thread checks
/// Call this from the main thread during static constructor
///
public static void InitializeMainThreadId()
{
try { mainThreadId = Thread.CurrentThread.ManagedThreadId; } catch { mainThreadId = 0; }
}
///
/// True when called from the Unity main thread. False when the main thread id was never
/// initialized — callers that need a "safe on main thread" fast path should treat that
/// as "not the main thread".
///
public static bool IsMainThread
{
get
{
try { return mainThreadId != 0 && Thread.CurrentThread.ManagedThreadId == mainThreadId; }
catch { return false; }
}
}
///
/// Invoke the given function on the Unity main thread and wait up to timeoutMs for the result.
/// Returns null on timeout or error; caller should provide a fallback error response.
///
public static object InvokeOnMainThreadWithTimeout(Func