Hi,

I have a code that checks if one specific process is running in the TaskManager with the code below. If not the function will start this process.
The code I use in a Form_Load event when the application starts. (Working code is below)

What I initially wonder is instead of a Form Application, I would only need some kind of file that should run this code. This means that you would double click a file that open/closes very fast while only running this code.

Is this possible to achieve somehow. I am not sure what possibilities there are to do this exactly?

      private void Form1_Load(object sender, EventArgs e)
        {
            bool getInfo = IsProcessOpen("testApp");
            if (getInfo == false)
            {
                //Start the application here
                String appLoc = "D:/Programs/testApp.exe";
                System.Diagnostics.Process startApplication = new System.Diagnostics.Process();
                try
                {
                    startApplication.StartInfo.FileName = appLoc;
                    startApplication.Start();
                }
                catch { }
            }
        }

        public bool IsProcessOpen(string name)
        {
            //here we're going to get a list of all running processes on the computer
            foreach (System.Diagnostics.Process clsProcess in System.Diagnostics.Process.GetProcesses())
            {
                String getName = clsProcess.ProcessName.ToString().ToLower();
                if (getName == name)
                {
                    //if the process is found to be running then we return a true
                    return true;
                }
            }
            //otherwise we return a false
            return false;
        }

You might have already solved this but you would probably benifit more from using a console app and just opening the other application depending on the result of your IsProcessOpen method.

It would be quicker because it doesn't have to initialise the UI componants of the Win Forms app and (like on the Win Form app) you can do Application.Exit where required.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.