tags:

views:

400

answers:

2

Hi:

I have a window form application written in c# lauch perl script.

Everything is working except one problem. When the perl script run, it runs as a process launch by c# application. There are some delays I put in the perl script to wait for messages from socket interface.

Because of those delays, when c# application runs the script, the GUI looks like not responding state. I was using Process class to run the script. My question is that is there way to gives control back to parent process, the c# application from perl script process?

I thought the process.start() in c# is forking a new process, which shouldn't affect the GUI or the c# application itself.

Here is my code to start the perl script: loop through all perl scripts... { Process myProcess = new Process(); MessageBox.Show((string)curScriptFileName);

            string ParentPath = findParentPath((string)curScriptFileName);

            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("perl.exe");
            myProcessStartInfo.Arguments = (string)(curScriptFileName);
            myProcessStartInfo.UseShellExecute = false;
            myProcessStartInfo.RedirectStandardOutput = true;
            myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            myProcessStartInfo.CreateNoWindow = true;
            myProcessStartInfo.WorkingDirectory = ParentPath;
            myProcess.StartInfo = myProcessStartInfo;
            myProcess.Start();

            // Read the standard output of the spawned process.
            output = myProcess.StandardOutput.ReadToEnd();
            //MessageBox.Show(output);
            //this.ScriptTestResultTextBox.AppendText(output);
            //Console.WriteLine(output);
            myProcess.WaitForExit();
        }
        this.ScriptTestResultTextBox.AppendText(output);

As you can see, I used to put the Text Box appending code inside the loop. I expected that i can get updated immediately. But now, with the delay, the GUI is not responding I have to update the text box after process exit. Is there a way to solve this problem?

Thanks for the help.

A: 

Please take a look at MSDN entry for ProcessStartInfo.RedirectStandardOutput property as it suggest to use BeginOutputReadLine.

Rubens Farias
Thanks for the help. I will try that.
+1  A: 

The problem is that when you call myProcess.StandardOutput.ReadToEnd(), you are causing the C# application to block and wait for the spawned process (the Perl program) to completely finish and exit. So even though you're correct that the Perl process can run separately and not affect the parent app, you've coded the parent app so that it can't keep running like you want it to.

The way to fix this is to use a separate thread or some kind of asynchronous method to collect the output, so that your main thread can keep running and handling window messages. The BeginOutputReadLine method suggested by @Rubens is one way to do this, or you could use a thread pool thread via QueueUserWorkItem, or even create a whole new thread. My suggestion would be to start with BeginOutputReadLine, and only use one of the other methods if that doesn't handle your needs.

Charlie