/// <summary> /// Queues a method for execution. The method executes when a thread pool thread becomes available. /// </summary> /// <returns>PooledThread object describing the thread and its status.</returns> /// <param name="a">The action to be executed in the thread.</param> public PooledThread QueueAction(Action a) { PooledThread pt = new PooledThread () { action = a }; lock (m_sync) { if (availableThreads > 0) { Thread th = new Thread(new ParameterizedThreadStart((_th) => { try { a(); } finally { ThreadWillFinish((Thread)_th); } })); pt.thread = th; m_currentThreads.Add (th); th.Start(th); } else { m_queuedActions.Enqueue (pt); } } return pt; }
void ThreadWillFinish(Thread th) { Action a = null; lock (m_sync) { if (m_queuedActions.Count > 0) { PooledThread pt = m_queuedActions.Dequeue(); if (pt.aborted) { ThreadWillFinish(th); } else { pt.thread = th; a = pt.action; } } else { m_currentThreads.Remove(th); } } if (a != null) { a(); ThreadWillFinish(th); } }
public LongRunningWork LongRunning(Action <object> action, object state, string name) { if (_pool.TryDequeue(out var pooled) == false) { MemoryInformation.AssertNotAboutToRunOutOfMemory(); pooled = new PooledThread(this); var thread = new Thread(pooled.Run, PlatformDetails.Is32Bits ? 512 * Constants.Size.Kilobyte : 0) { Name = name, IsBackground = true, }; thread.Start(); } pooled.StartedAt = DateTime.UtcNow; return(pooled.SetWorkForThread(action, state, name)); }
public LongRunningWork LongRunning(Action <object> action, object state, string name) { if (_pool.TryDequeue(out var pooled) == false) { MemoryInformation.AssertNotAboutToRunOutOfMemory(_minimumFreeCommittedMemory); pooled = new PooledThread(this); var thread = new Thread(pooled.Run) { Name = name, IsBackground = true, }; thread.Start(); } pooled.StartedAt = DateTime.UtcNow; return(pooled.SetWorkForThread(action, state, name)); }
/// <summary> /// Queues a method for execution. The method executes when a thread pool thread becomes available. /// </summary> /// <returns>PooledThread object describing the thread and its status.</returns> /// <param name="a">The action to be executed in the thread.</param> public PooledThread QueueAction(Action a) { PooledThread pt = new PooledThread() { action = a }; lock (m_sync) { if (availableThreads > 0) { #if UNITY_WSA && !ENABLE_IL2CPP && !UNITY_EDITOR Thread th = new System.Threading.Tasks.Task(a).ContinueWith((_th) => { ThreadWillFinish((Thread)_th); }); #else Thread th = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart((_th) => { try { a(); } finally { ThreadWillFinish((Thread)_th); } })); #endif pt.thread = th; m_currentThreads.Add(th); th.Start(th); } else { m_queuedActions.Enqueue(pt); } } return(pt); }