Click here to Skip to main content
14,865,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was wondering if someone can help me out with this code:

C#
Process VerifyProcces = new Process();
//get path here the verify.exe is
string path = Path.GetFullPath(".\\");
VerifyProcces.StartInfo.FileName = Path.Combine(path, "verify_chunk_transition.exe");

//VerifyProcces.StartInfo.CreateNoWindow = true;
VerifyProcces.StartInfo.UseShellExecute = false;
//VerifyProcces.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
VerifyProcces.StartInfo.RedirectStandardOutput = true;
VerifyProcces.StartInfo.RedirectStandardInput = true;

//set workderection to path were the chunks are
VerifyProcces.StartInfo.WorkingDirectory = _outputpath;
VerifyProcces.StartInfo.Arguments = ".\\\\ " + _framerate + " " + pid;
VerifyProcces.Start();

MyStreamReader msr_stdout = new MyStreamReader(VerifyProcces.StandardOutput);

Thread t_stdout = new Thread(msr_stdout.Go);

t_stdout.Start();
t_stdout.Join();

VerifyProcces.WaitForExit();
string output = msr_stdout.Text;
return output;


the problem i was walking in is that the interface is freezing, now i tried to use a backgroundworker but this won't start multiple process...

i need a simple fix for easy read this process out in a new thread witch won't crash my UI.

my thanks.

Wessel
Posted
Comments
lw@zi 14-May-14 7:25am
   
Any specific reason you are reading output in separate thread?
Wessel Beulink 14-May-14 7:56am
   
No i just tried to avoid working in same thread, because this gives me the trouble in the interface
BillWoodruff 14-May-14 7:32am
   
Have you put break-points in the code and determined that the code runs to completion ?
Wessel Beulink 14-May-14 7:52am
   
yes i have, the JOIN() and WAITFOREXIT() keep my UI freezing. but it restart directly because i got arround 10 requests on this process. if i remove the join and exit, i won't get any output anymore. the last function was first:<br>
 <br>
string output = verifyprocess.StandardOutput without waitforexit.<br>
this was bugging my UI to for some reason the program keeps edeting the string for each line, witch makes this thread to busy

1 solution

WaitForExit method call is the one that is telling main thread to wait till the process is exited. You can remove it, if possible, to keep UI thread active.

Asynchronously reading output:

C#
        private void button1_Click(object sender, EventArgs e)
        {

            using (Process process = new Process())
            {
                process.StartInfo.FileName = "Executable Location Here";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.RedirectStandardInput = true;
                process.EnableRaisingEvents = true;

                process.Exited += process_Exited;
                process.OutputDataReceived += ProccesOutputDataReceived;
                process.ErrorDataReceived += ProccesErrorDataReceived;

                process.Start();

                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
            }

        }

        void process_Exited(object sender, EventArgs e)
        {
            // Handle exit here
        }

        void ProccesErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            // Handle error here
        }
void ProccesOutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            // Handle output here using e.Data
        }


Code above may need modifications for complex situations involving multiple threads, quick processing in called process and other overheads.
   
v3
Comments
Wessel Beulink 14-May-14 7:54am
   
it will keep up the UI but it won't give me any output anymore. mabye you know a better way to get this output without a backgroundworker or in the same thread?
lw@zi 14-May-14 9:03am
   
You could start the entire process on another thread. You can also read asynchronously if that is a possibility.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900