Running multiple processes from a console application asynchronously

Posted on 2007-10-03
Last Modified: 2013-11-07
I have created a simple console application that sftp's files from remote server in C# 2003 Win xp env. This application calls a function FTPRemoteFile asynchronously, which runs a batch script in a new process. Now I want to update the status in my database as soon as I have downloaded the files from the remote server using this function. The problem is I am unable to do that because the main exe exits before these processes that are running the batch files exits. How do I wait for each new process that I have created from the main exe to exit, update the statuses for all these and then exit the main exe. Also given below is the code for that function
private bool FTPRemoteFile(string remotePath, string localPath, string batchFilePath, string hostName,string gudun,long uid)
                        this.procHash = new Hashtable();
                        this.eventHandled = false;
                        this.m_objSw = new StreamWriter(batchFilePath);
                        string sftpInit = ConfigurationSettings.AppSettings["SFTPInitializer"];
                        string sftpFlag = ConfigurationSettings.AppSettings["SFTPFlag"];
                        m_objSw.WriteLine(sftpInit + "\"" + this.m_strSftpPath + "\""+ sftpFlag + hostName +":" + remotePath + "/*.xml " + localPath);

                        this.m_objBtchExct = new Process();
                        m_objBtchExct.ProcessName = "BOSIProcess_" + gudun;
                        m_objBtchExct.StartInfo.FileName = batchFilePath;
                        m_objBtchExct.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                        m_objBtchExct.EnableRaisingEvents = true;
                        m_objBtchExct.Exited += new System.EventHandler(this.btchExecute_Exited);
                              return true;
                              return false;
                  catch(Exception procEx)
                        throw procEx;
                        this.m_objProcStInfo = null;

private void btchExecute_Exited(object sender, EventArgs e)
             this.eventHandled = true;
Question by:amitpanchal2105
    LVL 5

    Expert Comment


    Hope this helps.

    // Starting a new process.
    using System;
    using System.Diagnostics;

    public class StartProcess
    public static void Main()
    Process newProc = Process.Start("wordpad.exe");
    Console.WriteLine("New process started.");
    newProc.Close(); // free resources
    Console.WriteLine("New process ended.");

    Author Comment

    Thanks for such a quick reply Colemss but the problem is I want to capture the exit of every process that I am starting from my main exe, so that I can update the statuses in my tables accurately.Actually I am starting these processes asynchronously and each one downloads a set of files from a remote server and once it has completed i want to update my database that the files download have completed. Could you please help me with that. Thanks
    LVL 5

    Accepted Solution

    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now