How To: Start a process from c# code

Working for eMagid does require from time to time an original solution for a common, or more frequently non-common problem.

Problem at hand required us to build a Console Application, that intelligently crawl website, and scrap key information.
The follow up was to allow the admin to launch that application directly from their website.
There are three parts for the solution -

  1. Allowing the admin launch the console application
  2. Thread it! make sure that running the process doesn’t throw the website to a timeout .
  3. Ensuring that the application cannot be launch while a previous instance is still running.
Now let’s see how it should be done!



Running the process

In our case we had to provide the application with arguments, but that’s not always the case, so let’s create a method that can support either way.
All the following code does is run the application / process.

public void ExecuteApp(object o)
        {
            try
            {
                ProcessStartInfo ProcessInfo;
                System.Diagnostics.Process Process;

                ProcessInfo = new ProcessStartInfo("C:\MyApp.exe");

                // Check to see if there are arguments to pass along to the app
                if (o != null)
                {
                    ProcessInfo.Arguments = o.ToString();
                }

                // The console application will be launch, 'false' means that it will run without the user seeing a window popping up.
                ProcessInfo.CreateNoWindow = true;
                ProcessInfo.UseShellExecute = false;
                Process = System.Diagnostics.Process.Start(ProcessInfo);

            }catch
            {

            }

        }



Thread it

In this part we are making sure that the process runs as an independent thread (still inside the AppDomain, but it will allow us to continue using the site normally, without waiting for the ‘server side process’ to finish).

public void RunReader(object o)
{
    var ts = new ParameterizedThreadStart(ExecuteApp);

    (new Thread(ts)).Start(o);
}

public void ExecuteApp(object o)
        {
            try
            {
                ProcessStartInfo ProcessInfo;
                System.Diagnostics.Process Process;

                ProcessInfo = new ProcessStartInfo("C:\MyApp.exe");

                // Check to see if there are arguments to pass along to the app
                if (o != null)
                {
                    ProcessInfo.Arguments = o.ToString();
                }

                // The console application will be launch, 'false' means that it will run without the user seeing a window popping up.
                ProcessInfo.CreateNoWindow = true;
                ProcessInfo.UseShellExecute = false;
                Process = System.Diagnostics.Process.Start(ProcessInfo);

            }catch
            {

            }

        }



Last part – One is not necessarily a lonely number

So let’s make sure there is only one single instance of the process running at a time

Shall we start with adding a static flag?!

public static bool AppIsRunning = false;

And let’s put it all together.


        public static bool AppIsRunning = false;

        public void RunProcess(object o)
        {
            // Making sure the process is not already running.
            if(MyController.AppIsRunning)
                return ;

                var ts = new ParameterizedThreadStart(ExecuteReader);

                (new Thread(ts)).Start(o);
        }

        public void ExecuteProcess(object o)
        {
            try
            {
                // changing the 'current running' flag to true
                MyController.AppIsRunning = true;

                ProcessStartInfo ProcessInfo;
                System.Diagnostics.Process Process;

                ProcessInfo = new ProcessStartInfo("C:\MyApp.exe");

                // Check to see if there are arguments to pass along to the app
                if (o != null)
                {
                    ProcessInfo.Arguments = o.ToString();
                }

                // The console application will be launch, 'false' means that it will run without the user seeing a window popping up.
                ProcessInfo.CreateNoWindow = true;
                ProcessInfo.UseShellExecute = false;
                Process = System.Diagnostics.Process.Start(ProcessInfo);

                // Hey, that line wasn't there before !!
                // Well this is where we register the delegate to handle the event fired after the process finished its work.
                Process.Exited += new EventHandler(Process_Exited);
            }catch
            {
                // Obviously the process is no longer running.
                MyController.AppIsRunning = false;
            }

        }

        void Process_Exited(object sender, EventArgs e)
        {
            MyController.AppIsRunning = false;
        }

Speak Your Mind

*