How to limit the number of simultaneous processes?

My situation is as follows:

  • I have an application that can only be run a fixed number of times (less than 50).
  • A separate central process for managing other processes is not allowed due to business requirements. (i.e. if a good solution that includes ONLY application processes is still acceptable)
  • I use C # to develop the application, and so a managed solution is preferred.
  • I have to deal with "unforeseen" cases, such as processes can be terminated using the TaskManager.

I am thinking of a solution that uses a system-wide mutex. However, he does not survive the “unexpected” cases very well, as he leaves the “abandoned” mutex. If this is a good way, may I ask that the trick of "ignoring" the mutex is abandoned?

+2
source share
6 answers

One approach would be to request a list of processes and count the number of live instances. Another approach is likely to be UDP transmission and counting the number of responses. I used this template for distributed scripts related to job processors.

NTN

Colby africa

+2
source

, , , . , .

0

"" , , ThreadAbortException. - / , , .

100%, , - .

0

, WMI.NET #, :

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Management;

namespace WmiProc
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementScope ms = new System.Management.ManagementScope(
                @"\\myserver\root\cimv2");

            var oq = new System.Management.ObjectQuery(
                "SELECT * FROM Win32_Process where Name='myprocname'");
            ManagementObjectSearcher query1 = new ManagementObjectSearcher(ms, oq);
            ManagementObjectCollection procsCollection = query1.Get();
            Console.WriteLine("process count:{0}", procsCollection.Count);
        }
    }
}

: , . .

, () (, ).

0

, Mutex.

, , .

, , , , .

, .

class Program
{
    private static Mutex mutex = null;

    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);

        int count = Program.CheckInstanceCount();
        Console.WriteLine("This is instance {0} running.", count);
        Console.Read();
    }

    static void CurrentDomain_ProcessExit(object sender, EventArgs e)
    {
        Program.mutex.ReleaseMutex();
        Program.mutex.Close();
    }

    private static int CheckInstanceCount()
    {
        int result = 0;
        bool created = false;

        for (int i = 0; i < 50; i++)
        {
            /* try to create a mutex with this name, 
             * if it does exist, another instance 
             * of this program is running
             */
            mutex = new Mutex(true, string.Concat(AppDomain.CurrentDomain.FriendlyName, i.ToString()), out created);
            if (created)
            {
                // this instance is instance #i currently running
                result = i;
                break;
            }
        }
        return result;
    }
}
0

, , , , .

// You can use any System wide mutual exclusion mechanism here
bool waitAndLockMutex();
void unlockMutex();

// returns the number of processes who use the specified command
int getProcessCount();

void main() {
    try {
        waitAndLockMutex();
        if (getProcessCount() > MAX_ALLOWED)
            return;

        doUsualWork();

    } finally {
        unlockMutex();
    }
}

, , .NET

EDIT:

If you do not want to go along the path of counting the processes of interest, you can use the global mutex for this. Not sure if .NET provides this. But the bottom line is that you can get all the mutexes up to MAX, and in the process, if you get a Mutex that has not yet been created or ABANDONED, then you go ahead and let the process start, otherwise exit, saying if the maximum number is exceeded

void main() {
    for (int i = 0; i < MAX; ++i) {
        int status = TryToAcquireMutex("mutex" + i);
        continue if (status == locked);
        if (status == success || status == WAIT_ABANDONED) {
            doUsusalWork();
        }
    }
}
0
source

All Articles