-1

I want to run the "pocketsphinx_continuous.exe" program which is written in C language from my WPF application. Currently I run this using the command prompt. pocketsphinx_continuous.exe requires parameters.

The command will be something like this. Open cmd, go to the folder where "pocketsphinx_continuous.exe" exists and run,

pocketsphinx_continuous.exe -infile test\data\goforward.raw -hmm model\en-us\en-us -lm model\en-us\en-us.lm.bin -dict model\en-us\cmudict-en-us.dict

The program prints a string in command prompt. Instead of this I want to get this string to my WPF application.

Say, for a button click I want to run the above program and get the resulting string to a textbox in the same window. Running the C program should happen in the background.

How should I do this? Do I have to write a service between the C and WPF applications?

Kabilesh
  • 1,000
  • 6
  • 22
  • 47
  • 1
    This is a very common scenario, if you search you will find many examples. Here is one: https://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output – Jon Jan 05 '18 at 06:50

1 Answers1

1

You have to start a process with Process.Start() and redirect its console output. It's even possible to do this in the background and hide the console window.

Create a new process like this:

var process = new Process 
{
    StartInfo = new ProcessStartInfo 
    {
        FileName = "pocketsphinx_continuous.exe",
        Arguments = "-infile test\data\goforward.raw -hmm model\en-us\en-us -lm model\en-us\en-us.lm.bin -dict model\en-us\cmudict-en-us.dict",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};

Now you can start the process and redirect the console output:

process.Start();
while (!proc.StandardOutput.EndOfStream) 
{
    string line = process.StandardOutput.ReadLine();
}

line will contain a line of your program's output and you can do whatever you want with it.

lorisleitner
  • 688
  • 1
  • 5
  • 20