private bool DoWork() { _workIsDone.ManagedThreadId = Thread.CurrentThread.ManagedThreadId; if (_action == null) { // should only happen when we shutdown return(false); } ResetCurrentThreadName(); Thread.CurrentThread.Name = _name; try { _workIsDone.CurrentThreadStats = NativeMemory.CurrentThreadStats; LongRunningWork.Current = _workIsDone; _action(_state); } catch (Exception e) { if (_log.IsOperationsEnabled) { _log.Operations($"An uncaught exception occurred in '{_name}' and killed the process", e); } throw; } finally { _workIsDone.Set(); LongRunningWork.Current = null; } _action = null; _state = null; _workIsDone = null; NativeMemory.CurrentThreadStats.CurrentlyAllocatedForProcessing = 0; ThreadLocalCleanup.Run(); ResetCurrentThreadName(); Thread.CurrentThread.Name = "Available Pool Thread"; var resetThread = ResetThreadPriority(); resetThread &= ResetThreadAffinity(); if (resetThread == false) { return(false); } _waitForWork.Reset(); lock (_parent) { if (_parent._disposed) { return(false); } if (_parent._lowMemoryFlag.IsRaised()) { SetWorkForThread(null, null, null); return(false); } _parent._pool.Enqueue(this); } return(true); }
public void Run() { try { InitializeProcessThreads(); LongRunningWork.CurrentPooledThread = this; while (true) { _waitForWork.WaitOne(); _workIsDone.ManagedThreadId = Thread.CurrentThread.ManagedThreadId; if (_action == null) { return; // should only happen when we shutdown } ResetCurrentThreadName(); Thread.CurrentThread.Name = _name; try { LongRunningWork.Current = _workIsDone; _action(_state); } catch (Exception e) { if (_log.IsOperationsEnabled) { _log.Operations($"An uncaught exception occurred in '{_name}' and killed the process", e); } throw; } finally { _workIsDone.Set(); LongRunningWork.Current = null; } _action = null; _state = null; _workIsDone = null; ThreadLocalCleanup.Run(); ResetCurrentThreadName(); Thread.CurrentThread.Name = "Available Pool Thread"; if (ResetThreadPriority() == false) { return; } if (ResetThreadAffinity() == false) { return; } _waitForWork.Reset(); lock (_parent) { if (_parent._disposed) { return; } _parent._pool.Enqueue(this); } } } finally { NativeMemory.NotifyCurrentThreadAboutToClose(); } }