[MonoDevelop] Process with Mono

Michael Hutchinson m.j.hutchinson at gmail.com
Sun Nov 14 18:02:56 EST 2010


On Sun, Nov 14, 2010 at 5:49 PM, guybrush.d <thera at interfree.it> wrote:
>
> Hi everybody,
> Let me first excuse if my question has already been answered, but i didn't
> find it.
> OK My question is I'm trying to develop a gui application (GTK#) for
> Slackware,
> it will be a gui for slapt-get and more, i know that there are already other
> gui for slapt-get
> but mine will be with more functions. My problem is to read the standard
> output from console
> when i launch slapt-get, i've already been able to read it in asynchronous
> mode, pratically
> i call the command with "Process.Start" method and after a while i'm able to
> display the std out
> to a text view. What i'm not able to do is to display the std out while
> slapt-get is working. I've tried
> the treads method but i haven't been able to make it work, i have a couple
> of class on windows
> that fits perfectly my needs, i already used it for other apps, but they
> works with windows forms
> and i'm developing an application for gtk so the classes used are not
> available! I've googled everywhere,
> i've even tried to use the vte terminal, but i don't understand how to
> retrieve the output! Can anybody suggest me
> how to accomplish this? Here two pieces of code i used:
>
> This is the simple method i used it starts the program, and after the
> command has been
> executed, it shows me the result in the text view.
>  protected virtual void StartProcess()
> {
>        sortProcess.StartInfo.UseShellExecute = false;
>
>        // I redirect the standard output
>        sortProcess.StartInfo.RedirectStandardOutput = true;
>
>        sortProcess.StartInfo.FileName = @"/usr/sbin/slapt-get";
>        sortProcess.StartInfo.Arguments = " --list"; //--list";
>        sortProcess.StartInfo.WorkingDirectory = @".";
>        sortProcess.Start();
>
>        // I read the output to a string
>        stdOutput = sortProcess.StandardOutput.ReadToEnd();
>
>        // and then load the textview
>        textview1.Buffer.Text = stdOutput;
>        sortProcess.WaitForExit ();
>
> }
>
> Second Method i tried to used threads, but the result is almost the same:
> public void FirstUnsyncThreads ()
> {
>        // Crea due threads. The ThreadStart delegate is points to
>        // the method being run in a new thread.
>        Thread firstRunner = new Thread (new ThreadStart (this.firstRun));
>        Thread secondRunner = new Thread (new ThreadStart (this.secondRun));
>
>        // Starting our two threads. Thread.Sleep(10) gives the first Thread
>        // 10 miliseconds more time.
>        firstRunner.Start ();
>        Thread.Sleep (10);
>        secondRunner.Start ();
>
> }
>
> // This method is being excecuted on the first thread.
> public void firstRun ()
> {
>        sortProcess.StartInfo.UseShellExecute = false;
>        // we need to redirect the standard output so we read it
>        // internally in out program
>        sortProcess.StartInfo.RedirectStandardOutput = true;
>
>        sortProcess.StartInfo.FileName = @"/usr/sbin/slapt-get";
>        sortProcess.StartInfo.Arguments = " --list"; //--list";
>        sortProcess.StartInfo.WorkingDirectory = @".";
>        sortProcess.Start();
>
>        // read the output to a string
>        stdOutput = sortProcess.BeginOutputReadLine.ToString();
>
>        sortProcess.WaitForExit ();
>
>        Thread.Sleep (100);
>
> }
>
> // This method is being excecuted on the second thread.
> public void secondRun ()
> {
>        // finally output the string
>        textview1.Buffer.Text = stdOutput;
>
> }
>
> Ok guys that's all i'm in your hands, if i don't understand how make this
> work
> my app will be almost useless, this code will also make a progress bar
> increment
> but i need to get the std output in synchro else i won't work!!!Please HELP!
>
> PS.: Sorry for my english, if i made some mystake i'm just italian, CIAO.

That's not how BeginOutputReadLine works. What it does is to put the
process output into "async" mode, so that it dispatches the
OutputDataReceived event every time an line of output comes in. If you
do this, you cannot use the process output streams directly.

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx

If you wish to get async output for incomplete lines, you can start up
helper threads to do blocking reads on the output streams, like
MonoDevelop does with its ProcessWrapper class:

https://github.com/mono/monodevelop/blob/master/main/src/core/MonoDevelop.Core/MonoDevelop.Core.Execution/ProcessWrapper.cs

-- 
Michael Hutchinson
http://mjhutchinson.com


More information about the Monodevelop-list mailing list