• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

Running multiple processes from a console application asynchronously

I have created a simple console application that sftp's files from remote server in C# vs.net 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)
            {
                  try
                  {
                        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);
                        m_objSw.Flush();
                        m_objSw.Close();

                        if(!Directory.Exists(localPath))
                        {
                              Directory.CreateDirectory(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);
                        m_objBtchExct.Start();
                                                
                        if(eventHandled)
                        {
                              DataAccess.InsertBosiTable(this.m_strConnectionsString,gudun,uid,Convert.ToInt32(HelperEnum.FilesDownloadedSuccessfully),DateTime.Now,string.Empty);
                              //DataAccess.UpdateWebServiceEnd(this.m_strConnectionsString,ds.Tables[0].Rows[_end-1]["GUDUN"].ToString(),COMP);
                              //DataAccess.UpdateBOSIGUDUNQueue(this.m_strConnectionsString,ds.Tables[0].Rows[_end-1]["GUDUN"].ToString(),Convert.ToInt32(HelperQueueEnum.COMP));
                              return true;
                        }
                        else
                              return false;
                        
                  }
                  catch(Exception procEx)
                  {
                        throw procEx;
                  }
                  finally
                  {
                        this.m_objBtchExct.Close();
                        this.m_objProcStInfo = null;
                  }
            }

private void btchExecute_Exited(object sender, EventArgs e)
{
             this.eventHandled = true;
}
0
amitpanchal2105
Asked:
amitpanchal2105
  • 2
1 Solution
 
ColemssCommented:
http://www.thescripts.com/forum/thread660919.html
http://www.codeproject.com/csharp/start_an_external_app.asp

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.WaitForExit();
newProc.Close(); // free resources
Console.WriteLine("New process ended.");
}
}
0
 
amitpanchal2105Author Commented:
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
0
 
ColemssCommented:
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now