public void StopMining() { try { m_keepMining = false; Alarm.Clear(); MiningCommand = MinerProgramCommand.Stop; //clear both queues so that threads wint start running them DownloadingQueue.Clear(); //sometimes if downloaidnf thread is stuck in a long download and we want to stop we might hav to abort thread if (m_Downloading) { m_downloadingThread.Abort(); } MiningQueue.Clear(); RunningMiners.Clear(); if (ActiveMiner != null) { ActiveMiner.StopMining(); ActiveMiner = null; } } catch (Exception e) { } }
void DownLoadingThread(object obj) { IncrThreadCount(); while (m_keepRunning) { try { m_Downloading = false; if (DownloadingQueue.Count == 0 || m_keepMining == false) { Thread.Sleep(3000); } else { IMinerProgram miner = DownloadingQueue.Dequeue(); m_Downloading = true; miner.DownloadProgram(); m_Downloading = false; if (miner.ReadyForMining()) { MiningQueue.Enqueue(miner); } } } catch (ThreadAbortException e) { Logger.Instance.LogInfo("Downloading Thread has been stopped and will resume again!!"); Thread.ResetAbort(); } catch (Exception e) { Logger.Instance.LogError(e.Message); //if we feel the need to exit when there is an error in miner thread //m_keepRunning = false; } m_Downloading = false; } DecrThreadCount(); }
void MiningThread(object obj) { IncrThreadCount(); while (m_keepRunning)//thread runs as long as app is on { try { if (MiningQueue.Count == 0 || m_keepMining == false) { Thread.Sleep(3000); } else { IMinerProgram miner = MiningQueue.Dequeue(); if (miner.ReadyForMining()) { try { miner.StartMining(); RunningMiners.Add(miner); } catch (Exception e) { Logger.Instance.LogError(e.Message); } } else { DownloadingQueue.Enqueue(miner); } } if (m_keepMining) { try { List <IMinerProgram> stoppedMiners = new List <IMinerProgram>(); //Ensure all started miners are still running for (int i = 0; i < RunningMiners.Count; i++) { IMinerProgram item = RunningMiners[i]; if (!item.Running()) { item.SetRunningState(MinerProgramState.Stopped); //just to be sure. we never want to start miner twice item.KillMiner(); //Dont directly start mining. push it to queue and let the workflow start. also only if mining is stil on if (m_keepMining) { //MessageBox.Show(item.Miner.Name); MiningQueue.Enqueue(item); } stoppedMiners.Add(item); } } //if a miner has stopped, we need to remove it from the running list as anyway it will be added once run foreach (IMinerProgram item in stoppedMiners) { RunningMiners.Remove(item); } } catch (Exception e) { } } else { //Kill all running miners and go to sleep for some time for (int i = 0; i < RunningMiners.Count; i++) { IMinerProgram item = RunningMiners[i]; item.KillMiner(); } RunningMiners.Clear(); } } catch (Exception e) { Logger.Instance.LogError(e.Message); } } DecrThreadCount(); }
public MiningQueueTests() { _taskQueue = new MiningQueue(new Mock <IStatisticService>().Object); }