Question : How to implement Exited eventhandler of System.Diagnostics.Process?

hi,

I have the below code to invoke a process and it works fine as long as there is no exception from P_Exited().

The method "StartUploadProcess" will return true or false. Even if there is an exception from P_Exited(), "StartUploadProcess" is always returning "true".
I need to make it return false if there is an exception from P_Exited().

Thanks for any help.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace seasharpwinapp
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool status = StartUploadProcess();

            if (status)
            {
                MessageBox.Show("Upload successful");
            }
            else
            {
                MessageBox.Show("Upload failed");
            }
        }
        
        private bool StartUploadProcess()
        {
            try
            {
                Process P = new Process();
                P.EnableRaisingEvents = true;
                P.Exited += P_Exited;
                P.StartInfo.FileName = @"C:\\Temp\\Upload.cmd";
                P.StartInfo.Arguments = "C:\\Temp\\UploadScript.txt" + " " + "File1";
                P.Start();

                return true;                
            }


            catch (Exception x)
            {
                MessageBox.Show(string.Format("Error while uploading. {0} \r\n {1}", x.Message, x.InnerException.ToString()));
                return false;
            }
        }

        private void P_Exited(object sender, System.EventArgs e)
        {
            try
            {
                System.IO.File.Delete("C:\\Temp\\UploadFiles\\File1");
            }
            catch (Exception x)
            {
                MessageBox.Show(string.Format("There was a problem deleting the file located in {0}. {1}", "C:\\Temp\\UploadFile\\File1", x.Message));
            }

            try
            {
                System.IO.File.Delete("C:\\Temp\\UploadScript.txt");
            }
            catch (Exception x)
            {
                MessageBox.Show(string.Format("There was a problem deleting the file located in {0}. {1}", "C:\\Temp\\UploadScript.txt",x.Message));
            }
        }
        
    }
}

Answer : How to implement Exited eventhandler of System.Diagnostics.Process?

Replace line 46 with:
P.Start();
P.WaitForExit();
Random Solutions  
 
programming4us programming4us