private void DoWork(object arg) { _isRunning = true; while (_isRunning) { if (_mockResource.LastException != null) { Debug.WriteLine("DoWork is now shutdown by Resource LastException is not null."); break; } if (_shutdownEvent.WaitOne(0)) { Debug.WriteLine("DoWork is now shutdown by shutDownEvent."); break; } try { _mockResource.DoMethod(arg); if (arg.ToString() == "RiseLoopException") { throw new Exception("Loop Exception."); } } catch (Exception ex) { Debug.WriteLine("DoWork is now shutdown by Loop Exception. Message:" + ex.Message); break; } } /*Itt kezdődik a vége*/ if (_mockResource != null) { /*Erőforrás felszabadítása, itt a szabályos.*/ _mockResource.Dispose(); _mockResource = null; } /*Leáll és a Stop-nak jelzem, hogy mehet tovább.*/ _isRunning = false; _readyToDisposeEvent.Set(); Debug.WriteLine("Loop is colsed..."); }