1

I have one exe which will invoke four instances of other exe.So the Main method of second exe has following code.But there are situations like two invokes of (args[1]=="Test") will happen.I want to block second invoke ie, want to execute Both methods(firstmethod(),secondmethod()) one time only.How to do that these all athe four invokes

args[0]   =="leaf"         args[1] =="Test"
args[0]   =="seat"         args[1] =="Test"
args[0]   =="kite"         args[1] =="leveller"
args[0]   =="micky"         args[1] =="sasi"

and this is the code in second exe Main method

if (args[1]=="Test")
{
firstmethod();
secondmethod();
}
peter
  • 8,158
  • 21
  • 66
  • 119
  • I can't say I completely understand your question, but maybe just store a list of which processes (eg `Test`) have already been invoked and incorporating a check into part of your `if()`? (`if(args[1]=="Test" && !alreadyInvoked.Contains("Test"))`)- Again, I may be dead wrong, but don't understand any better what you're trying to accomplish. – John Bustos Mar 14 '16 at 19:01
  • I am talking about the receiving side ie second EXE.What you said is about the invoking side ie first EXE.That will not work here as i want 4 invokes of this second EXE and for this particular block only one time – peter Mar 14 '16 at 19:14
  • 2
    From what you've described it sounds like you'll need to use some combination of a Mutex with some means of IPC, perhaps something simple like a lock file, or mapped memory, to communicate that the process has been run. – khargoosh Mar 14 '16 at 21:47

1 Answers1

2

To restrict activity across multiple processes requires a Mutex. The workflow could look something like this:

second.exe process is started...

if (args[1]=="Test")
{
    bool mutexWasAcquired = AcquireMutex();
    if (mutexWasAcquired)
    {
        if (CheckIfMethodsHaveBeenRun() == false)
        {
           firstmethod();
           secondmethod();
           IndicateMethodsHaveBeenRun();
        }
        ReleaseMutex();
    }         
}

To indicate that the methods have been run, the first process to successfully run the methods could create a file which serves as an interprocess indicator. If first.exe is run again, it should check for and delete that file. That's just one example.

Note that the syntax required to get safe mutex use correct is specific and critical.

Community
  • 1
  • 1
khargoosh
  • 1,450
  • 15
  • 40