locked
C# calling a process RRS feed

  • Question

  • I have the following questions about a C# 2010 application calling another C# console application using the C# process method:
    1. When using a process method, I have seen where you can use the process as either a static method call and also an object that can be instantiated. What is the preferred method and why?
    2. If I instantiate a process as an object, do I need to delete that process in memory so that it does not stay around in memory? If so, how do you delete the process?
    3. The following code is a snippet of what I have setup so far. Can you tell me if you see any problems with this code? If so can you tell me how to modify the code so that it works better?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.ComponentModel;

    namespace eScripts
    {
    class Run_e
    {
    private static String strConsoleAppLocation = "C:\\Program Files (x86)\\coname\\1.exe";
    private static String strWebServiceurl = "https://etest//WebService1";
    static void Main(string[] args)
    {
    Process eRPT_Process = new Process();
    try
    {
    String Process_Arguments = null;
    eRPT_Process.StartInfo.UseShellExecute = false;
    eRPT_Process.StartInfo.FileName = strConsoleAppLocation;
    Process_Arguments = strWebServiceurl + " 164 " + cust1";
    eRPT_Process.StartInfo.Arguments = Process_Arguments;
    eRPT_Process.Start();
    eRPT_Process.WaitForExit(1800);
    Process_Arguments = null;
    eDataContext rptData = new eDataContext();
    string[] PkgIDs = rData.Trackings.Where(c => c.Received_Date != null
    .Select(c => c.Package_ID).ToArray();
    foreach (string PkgID in PkgIDs)
    {
    String Process1_Arguments = null;
    Process Process1 = new Process();
    Process1.StartInfo.FileName = strConsoleAppLocation;
    Process1_Arguments = strWebServiceurl + " 10 " + PkgID;
    Process1.StartInfo.Arguments = Process1_Arguments;
    Process1.Start();
    Process1.WaitForExit(1800);
    Process1.Dispose();
    Process1_Arguments = null;
    }
    ateTime StartDateTime = rData.Trackings.Where(c => c.Error_Report_Date != null).Select(c => c.Error_Report_Date).Max().GetValueOrDefault();
    DateTime CurDateTime = DateTime.Now;
    string[] AddPkgIDs = (from rData.Trackings.where(c.tNumber.Substring(0, 3) == "dis"
    select TNumb).Distinct().ToArray();
    if (AddPkgIDs != null)
    {
    foreach (string AddPkgID in AddPkgIDs)
    {
    String Process4_Arguments = null;
    Process Process4 = new Process();
    Process4.StartInfo.FileName = strConsoleAppLocation;
    Process4_Arguments = strWebServiceurl + " 14295 " + AddPkgID;
    Process4.StartInfo.Arguments = Process4_Arguments;
    Process4.Start();
    Process4.WaitForExit(1800); //waits 2 minutes
    Process4.Dispose();
    }
    }
    }

    catch (Exception e)
    {
    Console.WriteLine(e.Message);
    }
    }
    }
    Sunday, October 21, 2012 5:32 PM

Answers

  • I think that in simple cases this

    System.Diagnostics.Process.Start("thePathToProgramToStart.exe");

    will start any program just fine. This method has also some overloads that should cover the remaining occasions.

    1. I prefer to use the static method because I don't have to create any instances. For more info consult the MSDN library.

    2. I don't think so. C# has a GC (Garbage Collector).

    3. I didn't check the whole code. It seems but, but read 1. again.


    João Miguel

    • Marked as answer by jazz_dog Sunday, October 21, 2012 11:31 PM
    Sunday, October 21, 2012 6:36 PM