0

I'm writing an app that displays your current hashrate in Ethereum (A cryptocurrency like Bitcoin), and I need to somehow get the continuous output from the Command line that is running. This is what I have so far, but it is not printing to the program output:

 pProcess.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
    // Prepend line numbers to each line of the output.
    if (!String.IsNullOrEmpty(e.Data))
    {
        System.Console.Write(e.Data);
    }
});

//Wait for process to finish
pProcess.WaitForExit();

What is not working with this code? I'm guessing there's something messed up with the event handler, but I don't know what.

Noam M
  • 3,156
  • 5
  • 26
  • 41
MattyAB
  • 365
  • 2
  • 9
  • 19

1 Answers1

1

Copy and paste it to your code I modified it for you:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

    namespace ETHMinerVisualiser
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }

            private void MineButton_Click(object sender, RoutedEventArgs e)
            {
                Task.Run(() => { startMining(); });
            }

            public void startMining()
            {
                //Create process
                System.Diagnostics.Process pProcess = new System.Diagnostics.Process();

                //strCommand is path and file name of command to run
                pProcess.StartInfo.FileName = @"E:/Documents/ETH/ETHMinerVisualiser/ethminer-cuda-0.9.41-new/ethminer.exe";

                //strCommandParameters are parameters to pass to program
                pProcess.StartInfo.Arguments = "-F eu1.ethermine.org:5555/0x9c3f6281b123541f10c9bf37a8f273aa2a774d17.PCGPU -C";

                pProcess.StartInfo.UseShellExecute = false;


                //Set output of program to be written to process output stream
                pProcess.StartInfo.RedirectStandardOutput = true;

                //Optional
                pProcess.StartInfo.WorkingDirectory = "";

                //Start the process
                pProcess.Start();

                //pProcess.StartInfo.CreateNoWindow = true;

                //pProcess.BeginOutputReadLine();

                pProcess.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
                {
                    // Prepend line numbers to each line of the output.
                    if (!String.IsNullOrEmpty(e.Data))
                    {
                        //System.Console.Write(e.Data);                   
                        Debug.WriteLine(e.Data);
                    }
                });

                //Wait for process to finish
                pProcess.BeginOutputReadLine();

                pProcess.WaitForExit();
            }
        }
    }
bluetoothfx
  • 643
  • 9
  • 23
  • I don't get any output in the program out, only 'The thread 0x1448 has exited with code 0 (0x0)' – MattyAB Apr 06 '16 at 20:38
  • Correct this....I think you copied my location....pProcess.StartInfo.FileName = @"E:/Documents/ETH/ETHMinerVisualiser/ethminer-cuda-0.9.41-new/ethminer.exe"; – bluetoothfx Apr 06 '16 at 20:41
  • but... that's not what I want... I want the Program output mirroring the cmd output. What did you think I meant? did I not explain it correctly? sorry if so. – MattyAB Apr 06 '16 at 20:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108444/discussion-between-bluetoothfx-and-mattyab). – bluetoothfx Apr 06 '16 at 20:44