private void RunThread() { OnBeginService(); Utility.InvokeSafe(onBeginService); DateTime lastTime = DateTime.UtcNow; while (true) { if (CancellationToken.IsCancellationRequested) { break; } try { DateTime nowTime = DateTime.UtcNow; double deltaTimeMS = TimeUtility.GetDeltaTimeMS(lastTime, nowTime); if (deltaTimeMS >= UpdateIntervalMS) { lastTime = nowTime; double deltaTimeSec = TimeUtility.MSToSec(deltaTimeMS); UpdateService(deltaTimeSec); Utility.InvokeSafe(onUpdateService); } } catch (Exception ex) { NLog.LogManager.GetCurrentClassLogger().Error("Exception - {0} {1}", ex.Message, ex.StackTrace); } Thread.Sleep(1); } Utility.InvokeSafe(onEndService); OnEndService(); ThreadCompletion.TrySetResult(true); }