Header Ads

test

test codes

namespace RunningInstance
{
    using System;
    using System.Diagnostics;
    using System.Reflection;
 
    class Program
    {
        static void Main(string[] args)
        {
            if (RunningInstance())
            {
                Console.WriteLine("Another instance of this process was already running, exiting...");
                return;
            }
 
            // ...
        }
 
        static bool RunningInstance()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
 
            // Loop through the running processes in with the same name
            foreach (Process p in processes)
            {
                // Ignore the current process
                if (p.Id != current.Id)
                {
                    // Make sure that the process is running from the exe file.
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", @"\") == current.MainModule.FileName)
                    {
                        return true;
                    }
                }
            }
 
            return false;
        }
    }
}
 
However, suppose you have a multi function console application that accepts a dozen different command line arguments to perform different jobs, all of which run as separate scheduled tasks at overlapping intervals. If this is the case, then checking the list of running processes may well find your executable already running, but have no idea which command line argument was used to start it. I tried adding:
 
Console.WriteLine("Instance started with args: '{0}'", p.StartInfo.Arguments);
above the "return true" statement in RunningInstance() but it will not print the command line args used to start it. Lets suppose we add 2 classes to our project. Task1 and Task2. For the sake of simplicity, they both look something like this:
namespace RunningInstance
{
    using System;
    using System.Threading;
 
    public class Task1
    { 
        public void Start()
        {
            Console.WriteLine("Starting Task 1");
        }
    }
}
 
Task 2 is exactly the same, except it prints "Starting Task 2". If we keep our RunningInstance check in place Main() now looks like this:
 
        static void Main(string[] args)
        {
            if (RunningInstance())
            {
                Console.WriteLine("An instance of this application is already running. Exiting.");
                Console.ReadLine();
                return;
            }
 
            if(args.Length < 1)
            {
                Console.WriteLine("Unrecognized Command.");
                return;
            }
 
            switch (args[0])
            {
                case "-task1":
                    var t1 = new Task1();
                    t1.Start();
                    break;
                case "-task2":
                    var t2 = new Task2();
                    t2.Start();
                    break;
                default:
                    Console.WriteLine("Unrecognized Command.");
                    break;
            }
        }

No comments

please write your mail id for contact: