public static void Stop() { _no_mine_tmr.Stop(); if (_cmd != null) { try { if (_cmd.HasExited == false) { _cmd.CloseMainWindow(); _cmd.Close(); } } catch (Exception) { try { if (_cmd != null) //after compiling it seemed that gc automatically set it as null after calling Kill(), even if it fails from lack of permission. _cmd.Close(); } catch {} //silent catching } } _cmd = null; if (_miner != null) { try { if (_miner.HasExited == false) _miner.Kill(); } catch (Exception) { try { if (_miner != null) //after compiling it seemed that gc automatically set it as null after calling Kill(), even if it fails from lack of permission. _miner.Close(); } catch {} //silent catching } } _miner = null; if (IOManager != null) io_proc = null; io_proc = null; _megaHashPerSecond = -1; //represents that the miner is not mining. wont be updated till the first feedback from miner _shares = -1; _rejects = -1; }
/// <summary> /// Starts a controlled poclbm process. /// </summary> /// <param name="url">The mining pool url. e.g. slush's pool: `stratum.bitcoin.cz` or `https://stratum.bitcoin.cz` for specific url</param> /// <param name="port">Mining pool's port. e.g. slush's pool: 3333</param> /// <param name="username">Your worker's username. e.g. elibelash.worker1</param> /// <param name="password">Your worker's password.</param> /// <param name="hide">Should it launch hidden (in the background) or with a window</param> /// <param name="arguments">Extra arguments that you want to pass. Please do not pass the following: `--varbose`;</param> /// <exception cref="IOException">When file <see cref="MinerFilePath"/> doesn't exist</exception> /// <exception cref="InvalidOperationException">When the miner is already started</exception> public static void Start(string url, ushort port, string username, string password, bool hide = true ,string arguments = "-r1") { if (IsOpen) throw new InvalidOperationException("Unable to start because there is a miner already open"); if (File.Exists(MinerFilePath) == false) throw new IOException("Couldn't find poclbm.exe at "+MinerFilePath); #region logging, url, tmr , args and sinfo preparation var url_header = "http://"; if (url.Contains("://")) { url_header = url.Substring(0, url.IndexOf("://", StringComparison.OrdinalIgnoreCase)) + "://"; url = url.Replace(url_header, ""); } if (_logs == null && LogOutput) _logs = new List<string>(); if (_logs != null && LogOutput == false) { _logs.Clear(); _logs = null; } var args = string.Format("{0}{1}:{2}@{3}:{4}", url_header ,username, password, url, port); var sinfo = new ProcessStartInfo { FileName = "cmd.exe", WindowStyle = hide ? ProcessWindowStyle.Hidden : ProcessWindowStyle.Normal, CreateNoWindow = hide, UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true }; #endregion _cmd = new Process() { StartInfo = sinfo }; _cmd.Exited += (sender, eventArgs) => { _no_mine_tmr.Stop(); Stop(); }; //self disposer on user manual close. _cmd.Start(); io_proc = new ProcessIOManager(_cmd); io_proc.StartProcessOutputRead(); io_proc.StdoutTextRead += reader; //invoke to start using var a = _cmd.StandardOutput; var b = _cmd.StandardInput; //_miner.Exited wont invoke untill a call on HasEnded at any //part of the code has been called (ofc if the proc indeed exited). //for this case, we call IsOpen at a below half-sec interval to give averagly reliable respond to user manually closing the window. new Thread(() => { try { while (IsOpen) Thread.Sleep(350); } catch {} }).Start(); //this also dies with the program closing. io_proc.WriteStdin("@echo off"); io_proc.WriteStdin("cd " + MinerLocation); io_proc.WriteStdin(string.Format("{0} {1} {2}", MinerAppTarget, arguments, args)); new Thread((() => { _miner = BindToPoclbm(); if (_miner == null) { if (MinerCrashed != null) MinerCrashed(_logs, "Could not bind to the poclbm process. (It was not found)"); Stop(); } })).Start(); _no_mine_tmr.Start(); }