private Task <IList <Miner> > LoadMiner() { try { var miner = Miner.Miners; var loadedMiner = new List <Miner>(); var sb = new StringBuilder("Found miner:"); foreach (var m in miner) { if (File.Exists(m.Path)) { sb.AppendLine($"- {m}"); loadedMiner.Add(m); } else { Logger.Warning($"Could not load {m}"); } } Logger.Info(sb.ToString()); _miner = loadedMiner; MinerLoaded?.Invoke(_miner); return(Task.FromResult((IList <Miner>)loadedMiner)); } catch (Exception e) { throw new Exception("Could not load the miner", e); } }
public void Run() { try { var proxy = GetProxyAndPorts(); proxy.ContinueWith(i => { if (i.Status != TaskStatus.RanToCompletion) { return; } _proxy = i.Result.uri; ProxyFound?.Invoke(_proxy); }, _ctx); proxy.Wait(_ctx); var hardware = LoadHardware(); var miner = LoadMiner(); var benchmarks = LoadBenchmarksAsync(miner, hardware, proxy); hardware.ContinueWith(i => { if (i.Status != TaskStatus.RanToCompletion) { return; } _hardware = i.Result; HardwareLoaded?.Invoke(_hardware); }, _ctx); miner.ContinueWith(i => { if (i.Status != TaskStatus.RanToCompletion) { return; } _miner = i.Result; MinerLoaded?.Invoke(_miner); }, _ctx); benchmarks.ContinueWith(i => { if (i.Status != TaskStatus.RanToCompletion) { return; } _benchmarks = i.Result; BenchmarksLoaded?.Invoke(_benchmarks); }, _ctx); while (!_ctx.IsCancellationRequested) { try { var coinInfos = new CoinInfoLoader(new Uri(Uri, "mineableCoins")).LoadAsync(_ctx); var bestAlgorithms = GetBestAlgorithmsAsync(hardware, benchmarks, coinInfos); //bestAlgorithms.ContinueWith(i => //{ //}, _ctx); RunMiningInstancesAsync(bestAlgorithms, proxy).Wait(_ctx); _ctx.WaitHandle.WaitOne(TimeSpan.FromMinutes(1)); } catch (OperationCanceledException e) { if (e.CancellationToken.IsCancellationRequested) { continue; } Logger.Exception("The mining instance(s) stopped", e); _ctx.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)); } catch (Exception e) { Logger.Exception("Could not start the mining instances", e); _ctx.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)); } } } catch (Exception e) { Logger.Exception("Could not load the core", e); } Task.WaitAll(_miningInstances .Where(i => !i.Task.IsCanceled && !i.Task.IsCompleted && !i.Task.IsFaulted) .Select(i => i.Task).ToArray()); Logger.Debug("Stopped"); }