process.HasExited

Hi,

I'm trying to maintain a list of process which are started by an winform application. I would close any existing process by checking the process.HasExited property. I works for any IE which is being opened but, this did not wrokout for word process which is open. The process.HasExited gives me always true even the document is open at the background.

My example code snippet,

private void button2_Click(object sender, System.EventArgs e)

{

process.StartInfo.FileName = "winword";

process.StartInfo.Arguments = "\"C:\\Karthick files\\Test.doc\"";

process.Start();

}

private void Form1_Closed(object sender, System.EventArgs e)

{

MessageBox.Show(process.HasExited.ToString());

}

Please could anyone of you help me out

Thanks in advance,

Karthick.



Answer this question

process.HasExited

  • Richard Morgan

    Is there any way to handle this Because my main application opens a process which needs to be closed on closing my application.

    Thanks in advance,

    Karthick.


  • Nataraj

    I'm using .NET 1.1. I need to maintian an instance of the process and hence can't use Process.Start static method.


  • Johnny2006

    Strange problem and I can't reproduce it with .NET 2.0. Looks like you are using .NET 1.x though. Only suggestion: make absolutely sure you start the process with Process.Start() and not Process.Start("winword").


  • Soonyu

    public class Form1 : System.Windows.Forms.Form

    {

    private AxSHDocVw.AxWebBrowser axWebBrowser1;

    private System.Windows.Forms.Button button1;

    private Object oDocument;

    private System.Windows.Forms.OpenFileDialog openFileDialog1;

    private System.Windows.Forms.Button button2;

    /// <summary>

    /// Required designer variable.

    /// </summary>

    private System.ComponentModel.Container components = null;

    public Form1()

    {

    InitializeComponent();

    }

    /// <summary>

    /// Clean up any resources being used.

    /// </summary>

    protected override void Dispose( bool disposing )

    {

    if( disposing )

    {

    if (components != null)

    {

    components.Dispose();

    }

    }

    base.Dispose( disposing );

    }

    #region Windows Form Designer generated code

    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    private void InitializeComponent()

    {

    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));

    this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();

    this.button1 = new System.Windows.Forms.Button();

    this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();

    this.button2 = new System.Windows.Forms.Button();

    ((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();

    this.SuspendLayout();

    //

    // axWebBrowser1

    //

    this.axWebBrowser1.Enabled = true;

    this.axWebBrowser1.Location = new System.Drawing.Point(32, 32);

    this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));

    this.axWebBrowser1.Size = new System.Drawing.Size(640, 264);

    this.axWebBrowser1.TabIndex = 0;

    this.axWebBrowser1.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser1_NavigateComplete2);

    //

    // button1

    //

    this.button1.Location = new System.Drawing.Point(248, 320);

    this.button1.Name = "button1";

    this.button1.Size = new System.Drawing.Size(152, 32);

    this.button1.TabIndex = 1;

    this.button1.Text = "load";

    this.button1.Click += new System.EventHandler(this.button1_Click);

    //

    // button2

    //

    this.button2.Location = new System.Drawing.Point(456, 328);

    this.button2.Name = "button2";

    this.button2.Size = new System.Drawing.Size(152, 24);

    this.button2.TabIndex = 2;

    this.button2.Text = "Make word transparent";

    this.button2.Click += new System.EventHandler(this.button2_Click);

    //

    // Form1

    //

    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

    this.ClientSize = new System.Drawing.Size(704, 366);

    this.Controls.Add(this.button2);

    this.Controls.Add(this.button1);

    this.Controls.Add(this.axWebBrowser1);

    this.Name = "Form1";

    this.Text = "Form1";

    this.Load += new System.EventHandler(this.Form1_Load);

    this.Closed += new System.EventHandler(this.Form1_Closed);

    ((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();

    this.ResumeLayout(false);

    }

    #endregion

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main()

    {

    Application.Run(new Form1());

    }

    private void button1_Click(object sender, System.EventArgs e)

    {

    String strFileName;

    //Find the Office document.

    openFileDialog1.FileName = "";

    openFileDialog1.ShowDialog();

    strFileName = openFileDialog1.FileName;

    //If the user does not cancel, open the document.

    if(strFileName.Length != 0)

    {

    Object refmissing = System.Reflection.Missing.Value;

    oDocument = null;

    axWebBrowser1.Navigate(strFileName, ref refmissing , ref refmissing , ref refmissing , ref refmissing);

    }

    }

    public void Form1_Load(object sender, System.EventArgs e)

    {

    button1.Text = "Browse";

    openFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt" ;

    openFileDialog1.FilterIndex = 1;

    }

     

    protected override void OnKeyPress(KeyPressEventArgs e)

    {

    e.Handled=true;

    base.OnKeyPress (e);

    }

    public void Form1_Closed(object sender, System.EventArgs e)

    {

    oDocument = null;

    Application.DoEvents();

    // GC.Collect();

    }

    public void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)

    {

    //Note: You can use the reference to the document object to

    // automate the document server.

    Object o = e.pDisp;

    oDocument = o.GetType().InvokeMember("Document",BindingFlags.GetProperty,null,o,null);

    Object oApplication = o.GetType().InvokeMember("Application",BindingFlags.GetProperty,null,oDocument,null);

    Object oName = o.GetType().InvokeMember("Name",BindingFlags.GetProperty ,null,oApplication,null);

    MessageBox.Show("File opened by: " + oName.ToString() );

    }

     

    }


  • Tryin2Bgood

    see this is a known problem if ur opening a winword instance.

    The winword will remain in memory unless its explicitly close.

    u just try calling notepad.exe or ur own exe it will work fine


  • MLee11

    I've tried your snippet in a .NET 1.1 project and it seems to be working fine. Perhaps you should debug on the process id you're trying to check.

    E.g. insert this line:

    System.Diagnostics.Trace.WriteLine(process.Id);

    in your code and see if it returns the same Id in both methods.


  • process.HasExited