public void StopMiner()
        {
            if (!_process.IsRunning())
            {
                _process = null;
                return;
            }

            LogActivity(_donationMiningMode == MiningModeEnum.Donation ? "DonationStop" : "Stop");
            WriteConsole(string.Format("Stopping {0} {1}", _currentRunning.ServicePrint, _currentRunning.AlgoName), true);
            RecordMiningTime();
            if (MinerKillMode == 0)
            {
                ProcessUtil.KillProcess(_process);
            }
            else
            {
                ProcessUtil.KillProcessAndChildren(_process.Id);
            }

            _process            = null;
            _donationMiningMode = MiningModeEnum.Stopped;

            if (_currentRunning != null)
            {
                PriceEntryBase entry = PriceEntries.Single(o => o.Id == _currentRunning.Id);
                entry.UpdateStatus();
            }

            if (_stoppedMining == null)
            {
                _stoppedMining = DateTime.Now;
            }
            _currentRunning = null;
        }
        private void RecordMiningTime()
        {
            if (_currentRunning == null || !_startMining.HasValue)
            {
                return;
            }

            if (_donationMiningMode == MiningModeEnum.Automatic)
            {
                _autoMiningTime += (DateTime.Now - _startMining.Value);
            }
            if (_donationMiningMode == MiningModeEnum.Donation)
            {
                _donationMiningTime += (DateTime.Now - _startMining.Value);
            }

            _currentRunning.TimeMining += (DateTime.Now - _startMining.Value);
            _currentRunning.UpdateStatus();
            _startMining = null;
        }
        private void StartMiner(PriceEntryBase entry, bool isMinimizedToTray = false)
        {
            _nextRun         = null;
            _nextRunFromTime = null;
            _currentRunning  = entry;
            _startMining     = DateTime.Now;
            _stoppedMining   = null;

            _process = new Process();
            if (_donationMiningMode == MiningModeEnum.Donation)
            {
                if (!string.IsNullOrWhiteSpace(entry.DonationFolder))
                {
                    _process.StartInfo.WorkingDirectory = entry.DonationFolder;
                }
                _process.StartInfo.FileName = string.IsNullOrWhiteSpace(entry.DonationFolder)
                    ? entry.DonationCommand
                    : string.Format(@"{0}\{1}", entry.DonationFolder, entry.DonationCommand);
                _process.StartInfo.Arguments = entry.DonationArguments;
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(entry.Folder))
                {
                    _process.StartInfo.WorkingDirectory = entry.Folder;
                }
                _process.StartInfo.FileName = string.IsNullOrWhiteSpace(entry.Folder)
                    ? entry.Command
                    : string.Format(@"{0}\{1}", entry.Folder, entry.Command);
                _process.StartInfo.Arguments = entry.Arguments;
            }

            WriteConsole(
                string.Format("Starting {0} {1} with {2} {3}", _currentRunning.ServicePrint, _currentRunning.Name,
                              _process.StartInfo.FileName, _process.StartInfo.Arguments), true);

            if (!string.IsNullOrWhiteSpace(_process.StartInfo.WorkingDirectory) &&
                !Directory.Exists(_process.StartInfo.WorkingDirectory))
            {
                entry.DeadTime = DateTime.Now;
                string message = string.Format("Path '{0}' does not exist.", _process.StartInfo.WorkingDirectory);
                _process = null;
                WriteConsole(message, true);
                throw new ArgumentException(message);
            }
            if (!string.IsNullOrWhiteSpace(_process.StartInfo.FileName) && !File.Exists(_process.StartInfo.FileName))
            {
                entry.DeadTime = DateTime.Now;
                string message = string.Format("File '{0}' does not exist.", _process.StartInfo.FileName);
                _process = null;
                WriteConsole(message, true);
                throw new ArgumentException(message);
            }

            if (entry.UseWindow)
            {
                _process.StartInfo.WindowStyle = (isMinimizedToTray && TrayMode == 2)
                    ? ProcessWindowStyle.Hidden
                    : ProcessWindowStyle.Minimized;
                _process.Start();

                Thread.Sleep(100);
                try
                {
                    ProcessUtil.SetWindowTitle(_process, string.Format("{0} {1} Miner", entry.ServicePrint, entry.Name));
                }
                catch (Exception ex)
                {
                    ErrorLogger.Log(ex);
                }

                if (isMinimizedToTray && TrayMode == 1)
                {
                    HideMinerWindow();
                }
            }
            else
            {
                _process.StartInfo.RedirectStandardOutput = true;
                _process.StartInfo.RedirectStandardError  = true;
                _process.EnableRaisingEvents       = true;
                _process.StartInfo.CreateNoWindow  = true;
                _process.StartInfo.UseShellExecute = false;

                _process.ErrorDataReceived  += ProcessConsoleOutput;
                _process.OutputDataReceived += ProcessConsoleOutput;

                _process.Start();

                _process.BeginOutputReadLine();
                _process.BeginErrorReadLine();
            }

            ProcessPriorityClass processPriority;

            if (entry.Priority != "Normal" && entry.Priority != string.Empty &&
                Enum.TryParse(entry.Priority, out processPriority))
            {
                // Defaults to Normal, other possible values are Idle, BelowNormal,
                // AboveNormal, High & RealTime. ccminer <3 RealTime
                // Note 1: Realtime by minercontrol is only possible when given administrator privileges to minercontrol
                // Note 2: --cpu-priority by ccminer overrides minercontrols priority
                // Note 3: When giving administrator privileges to minercontrol and setting the priority by minercontrol to
                // something DIFFERENT than what's used by --cpu-priority by ccminer, then your whole system locks up
                _process.PriorityClass = processPriority;
            }

            if (entry.Affinity > 0)
            {
                // Just like with start /affinity, you can use 1 for first core, 2 for second core, 4 for third core, etc
                _process.ProcessorAffinity = (IntPtr)entry.Affinity;
            }

            _startMining        = DateTime.Now;
            _donationMiningMode = MiningMode;

            entry.UpdateStatus();

            LogActivity(_donationMiningMode == MiningModeEnum.Donation ? "DonationStart" : "Start");
        }
Example #4
0
        private void StartMiner(PriceEntryBase entry, bool isMinimizedToTray = false)
        {
            _nextRun         = null;
            _nextRunFromTime = null;
            _currentRunning  = entry;
            _startMining     = DateTime.Now;

            _process = new Process();
            if (_donationMiningMode == MiningModeEnum.Donation)
            {
                if (!string.IsNullOrWhiteSpace(entry.DonationFolder))
                {
                    _process.StartInfo.WorkingDirectory = entry.DonationFolder;
                }
                _process.StartInfo.FileName = string.IsNullOrWhiteSpace(entry.DonationFolder)
                    ? entry.DonationCommand
                    : string.Format(@"{0}\{1}", entry.DonationFolder, entry.DonationCommand);
                _process.StartInfo.Arguments = entry.DonationArguments;
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(entry.Folder))
                {
                    _process.StartInfo.WorkingDirectory = entry.Folder;
                }
                _process.StartInfo.FileName = string.IsNullOrWhiteSpace(entry.Folder)
                    ? entry.Command
                    : string.Format(@"{0}\{1}", entry.Folder, entry.Command);
                _process.StartInfo.Arguments = entry.Arguments;
            }

            WriteConsole(string.Format("Starting {0} {1} with {2} {3}", _currentRunning.ServicePrint, _currentRunning.Name, _process.StartInfo.FileName, _process.StartInfo.Arguments), true);

            if (!string.IsNullOrWhiteSpace(_process.StartInfo.WorkingDirectory) && !Directory.Exists(_process.StartInfo.WorkingDirectory))
            {
                entry.DeadTime = DateTime.Now;
                var message = string.Format("Path '{0}' does not exist.", _process.StartInfo.WorkingDirectory);
                _process = null;
                WriteConsole(message, true);
                throw new ArgumentException(message);
            }
            if (!string.IsNullOrWhiteSpace(_process.StartInfo.FileName) && !File.Exists(_process.StartInfo.FileName))
            {
                entry.DeadTime = DateTime.Now;
                var message = string.Format("File '{0}' does not exist.", _process.StartInfo.FileName);
                _process = null;
                WriteConsole(message, true);
                throw new ArgumentException(message);
            }

            if (entry.UseWindow)
            {
                _process.StartInfo.WindowStyle = (isMinimizedToTray && TrayMode == 2) ? ProcessWindowStyle.Hidden : ProcessWindowStyle.Minimized;
                _process.Start();

                Thread.Sleep(100);
                try
                {
                    ProcessUtil.SetWindowTitle(_process, string.Format("{0} {1} Miner", entry.ServicePrint, entry.Name));
                }
                catch (Exception ex)
                {
                    ErrorLogger.Log(ex);
                }

                if (isMinimizedToTray && TrayMode == 1)
                {
                    HideMinerWindow();
                }
            }
            else
            {
                _process.StartInfo.RedirectStandardOutput = true;
                _process.StartInfo.RedirectStandardError  = true;
                _process.EnableRaisingEvents       = true;
                _process.StartInfo.CreateNoWindow  = true;
                _process.StartInfo.UseShellExecute = false;

                _process.ErrorDataReceived  += ProcessConsoleOutput;
                _process.OutputDataReceived += ProcessConsoleOutput;

                _process.Start();

                _process.BeginOutputReadLine();
                _process.BeginErrorReadLine();
            }

            _startMining        = DateTime.Now;
            _donationMiningMode = MiningMode;

            entry.UpdateStatus();

            LogActivity(_donationMiningMode == MiningModeEnum.Donation ? "DonationStart" : "Start");
        }