Click here to Skip to main content
15,916,412 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

please fint the Below Code some time it work fine but some time it Exit the Proce below is the Code
C#
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
   BackgroundWorker worker = sender as BackgroundWorker;
   string result;
   ProcessStartInfo start = new ProcessStartInfo();
   string myExeDir = (new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location)).Directory + @"\" + "Python.exe";
   start.FileName = myExeDir;
   start.Arguments = (Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\" + "TA" + @"\" + "Tool" + @"\" + strSelectedTreeNade + ".py");
   start.ErrorDialog = false;
   start.UseShellExecute = false;
   start.RedirectStandardError = true;
   start.RedirectStandardInput = true;
   start.RedirectStandardOutput = true;
   start.CreateNoWindow = true;
   StreamReader reader = null;
   //listLog.Items.Clear();
   listLog.Invoke(new Action(() => listLog.Items.Clear()));
   // reading the file
   string str_TestSuiteName1 = strSelectedTreeNade;
   List<string> m_CombinedTestsuiteNames = new List<string>();
   using (Process process = Process.Start(start))
   {
      using (reader = process.StandardOutput)
      {
         // ...
      }
   }
}

the proces.StandardOutput Become Null..

[Edit]
- Added pre tag to have correct code formatting
- Replaced "\\" with @"\" equivalent so that string delimiters are not messed up
[/Edit]
Posted
Updated 25-Feb-14 22:27pm
v2

1 solution

I'm not sure but your error may be related to this line:
Quote:
using (reader = process.StandardOutput)

'using' will try to dispose process.StandardOutput at the enclosing brace. But, I expect that it is the responsibility of 'Process'.
See the following example. You can adapt it to your code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test_ProcStart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitOthers();
        }

        private void InitOthers()
        {
            this.bg.DoWork += bg_DoWork;
            this.textBox_Input.Text = @"cmd /C dir d:\";
        }


        void bg_DoWork(object sender, DoWorkEventArgs e)
        {
            string a = e.Argument.ToString();
            string fn = a.Split(' ').First();
            string args = a.Substring(a.IndexOf(' ') + 1);
            // WORKS WELL
            ProcessStartInfo si = new ProcessStartInfo()
            {
                FileName = fn,
                Arguments = args,
                CreateNoWindow = true,
                RedirectStandardError = true,
                RedirectStandardInput = false,
                RedirectStandardOutput = true,
                UseShellExecute = false
            };
            using (Process p = new Process())
            {
                p.StartInfo = si;
                p.Start();
                this.Invoke(new Action(() =>
                {
                    this.textBox_Output.Text = p.StandardOutput.ReadToEnd();
                }));
                this.Invoke(new Action(() =>
                {
                    this.textBox_Error.Text = p.StandardError.ReadToEnd();
                }));
            }
        }

        BackgroundWorker bg = new BackgroundWorker();

        private void button_Run_Click(object sender, EventArgs e)
        {
            bg.RunWorkerAsync(this.textBox_Input.Text);
        }
    }
}
 
Share this answer
 
Comments
rahuls1 26-Feb-14 7:04am    
ya above mentio line is giving Proble can you explain more..
Vedat Ozan Oner 26-Feb-14 8:14am    
I'm sorry, I couldn't understand what you want exactly from me. On what part do you want more explanation?

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