Cassie Kasandr
Cassie Kasandr

Reputation: 341

Starting up several programs at once

I have written a small program (ProcessSample) to launch another programs that are defined in the .txt file (separated by new line).

The code is mostly taken from MSDN. I am just starting my programming adventure but I want to write something useful.

I don't know the smart way to run for example two programs simultaneously from my ProcessSample program.

In my .txt file I have just paths to programs with .exe. It's all working fine but my program is only running one program at the time. I thought I would run foreach but of course it won't work here as it just runs the first program and it waits till I exit it, then it will run the next one.

So I know the reason why is it not working. I just want to know how could I make it work the way I would like.

My C# code:

using System;
using System.Diagnostics;
using System.Threading;

namespace ProcessSample
{
    class ProcessMonitorSample
    {
        public static void Main()
        {
            Console.BufferHeight = 25;

            // Define variables to track the peak memory usage of the process. 
            long peakWorkingSet = 0;

            string[] Programs = System.IO.File.ReadAllLines(@"C:\Programs\list.txt");

            foreach (string Program in Programs)
            {
                Process myProcess = null;

                // Start the process.
                myProcess = Process.Start(@Program);

                // Display the process statistics until 
                // the user closes the program. 
                do
                {
                    if (!myProcess.HasExited)
                    {
                        // Refresh the current process property values.
                        myProcess.Refresh();

                        // Display current process statistics.
                        Console.WriteLine();
                        Console.WriteLine("Path: {0}, RAM: {1}", Program, (myProcess.WorkingSet64 / 1024 / 1024));

                        // Update the values for the overall peak memory statistics.
                        peakWorkingSet = myProcess.PeakWorkingSet64;

                        if (myProcess.Responding)
                        {
                            Console.WriteLine("Status: Running");
                        }
                        else
                        {
                            Console.WriteLine("Status: Not Responding!");
                        }

                        // Wait 2 seconds
                        Thread.Sleep(2000);
                    } // if
                } // do
                while (!myProcess.WaitForExit(1000));

                Console.WriteLine(); 
                Console.WriteLine("Process exit code: {0}", myProcess.ExitCode);
                Console.WriteLine("Peak physical memory usage of the process: {0}", (peakWorkingSet / 1024 / 1024));

                Console.WriteLine("Press any key to exit.");
                System.Console.ReadKey();
           } // foreach
        } // public
    } //class
} // namespace

Upvotes: 1

Views: 120

Answers (3)

Cassie Kasandr
Cassie Kasandr

Reputation: 341

Actually I have learned about Threads and I have used them for my program like this:

class Program
{
static void Main(string[] args)
{
    string[] myApps = { "notepad.exe", "calc.exe", "explorer.exe" };

    Thread w;
    ParameterizedThreadStart ts = new ParameterizedThreadStart(StartMyApp);
    foreach (var myApp in myApps)
    {
        w = new Thread(ts);
        w.Start(myApp);
        Thread.Sleep(1000);
    }
}

private static void StartMyApp(object myAppPath)
{
    ProcessStartInfo myInfoProcess = new ProcessStartInfo();
    myInfoProcess.FileName = myAppPath.ToString();
    myInfoProcess.WindowStyle = ProcessWindowStyle.Minimized;
    Process myProcess = Process.Start(myInfoProcess);

    do
    {
        if (!myProcess.HasExited)
        {
            myProcess.Refresh(); // Refresh the current process property values.
            Console.WriteLine(myProcess.ProcessName+" RAM: "+(myProcess.WorkingSet64 / 1024 / 1024).ToString()+"\n");
            Thread.Sleep(1000);
        }
    }
    while (!myProcess.WaitForExit(1000)); 
}

}

Upvotes: 1

Abhinav Ranjan
Abhinav Ranjan

Reputation: 146

You can replace your foreach with Parallel.ForEach to achieve what you want.

 Parallel.ForEach<String>(Programs, Program =>
            {
                Process myProcess = null;

                // Start the process.
                myProcess = Process.Start(@Program);

                // Display the process statistics until 
                // the user closes the program. 
                do
                {
                    if (!myProcess.HasExited)
                    {
                        // Refresh the current process property values.
                        myProcess.Refresh();

                        // Display current process statistics.

                        Console.WriteLine();
                        Console.WriteLine("Path: {0}, RAM: {1}", Program, (myProcess.WorkingSet64 / 1024 / 1024));

                        // Update the values for the overall peak memory statistics.
                        peakWorkingSet = myProcess.PeakWorkingSet64;

                        if (myProcess.Responding)
                        {
                            Console.WriteLine("Status: Running");
                        }
                        else
                        {
                            Console.WriteLine("Status: Not Responding!");
                        }

                        // Wait 2 seconds
                        Thread.Sleep(2000);
                    }
                }
                while (!myProcess.WaitForExit(1000));

                Console.WriteLine();
                Console.WriteLine("Process exit code: {0}", myProcess.ExitCode);
                Console.WriteLine("Peak physical memory usage of the process: {0}", (peakWorkingSet / 1024 / 1024));

                Console.WriteLine("Press any key to exit.");
                System.Console.ReadKey();
            });

Upvotes: 0

peter
peter

Reputation: 15109

The problem is in the inner while loop. There you are getting statistics from the running process and displaying them in the console. As far as I understand from your post, you don't need this feature so you can remove it and you would get:

using System;
using System.Diagnostics;
using System.Threading;

namespace ProcessSample
{
    class ProcessMonitorSample
    {
        public static void Main()
        {
            Console.BufferHeight = 25;

            // Define variables to track the peak memory usage of the process. 
            long peakWorkingSet = 0;

            string[] Programs = System.IO.File.ReadAllLines(@"C:\Programs\list.txt");

            foreach (string Program in Programs)
            {
                Process myProcess = null;

                // Start the process.
                myProcess = Process.Start(@Program);

                Console.WriteLine("Program started: {0}", Program);

            }
        }
    }
}

Upvotes: 1

Related Questions