Most efficient way to see if a process is still running in C#?

Discussion in 'Mixed Languages' started by CODYQX4, Oct 8, 2011.

  1. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,801
    44,854
    150
    #1 CODYQX4, Oct 8, 2011
    Last edited: Oct 8, 2011
    I have an app called MultiKMS that replaces the AutoKMS Service of older OTK, and it runs and starts vbc.exe. I find every now and then vbc.exe stops running, as in disappears. I have ESET installed and I'm not sure if that is it, as it reports nothing and doesn't stop me from restarting it.

    Thing is, MultiKMS.exe is going to be running idle always, so I figured I could just have it check if it stopped and restart it.

    Is there a better, more efficient way than just running a background worker, checking if the PID of the process is there, starting it if not, and putting the thing to sleep for awhile before checking again?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Daz

    Daz MDL Developer / Admin
    Staff Member

    Jul 31, 2009
    9,304
    66,111
    300
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,801
    44,854
    150
    Interesting, though this sounds like it would be used to restart my app, whereas I'm trying to restart KMSEmulator which is injected into vbc.exe's memory space. If my app dies, AV is likely at fault and the service gets deleted. I'm thinking about restarting KMSEmulator every so often.

    So far, I just rigged the service to do a do while loop, it will read the config file, trigger a start of KMSEmulator(s), which will just not run if its already running (note I can run one on each port), then sleep for 30 seconds and loop again. I'd assume a value of more than 30 seconds would be desirable, what would you recommend?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. Daz

    Daz MDL Developer / Admin
    Staff Member

    Jul 31, 2009
    9,304
    66,111
    300
    The longer the wait time the better IMO. That's if it's not critical for the file to be running within even 5 minutes?

    You're right that my suggestion wouldn't work in your case. If the keygen was your own executable that was getting stopped then it would probably work but as it's not I think your current method would be the most efficient.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,801
    44,854
    150
    I honestly think I should set it to 5-10 minutes or more, but maybe add a process monitor (I know all PIDs), and activate if that dies. But that radically complicates my very simple service and I'd rather it not have to receive an event or all that. I'll probably set a hier time and be done with it. I'm basically handling the chance of the KMS stopping. My vbc.exe processes seem to disappear without any idea why. AV doesn't scan or log an issue, and the app is not leaving signs of a crash.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,097
    120
    #6 Josh Cell, Oct 8, 2011
    Last edited by a moderator: Apr 20, 2017
    Well, You can use it to view if process it running and Kill it :

    Code:
    public bool ProcessFinderAndKiller(string process)
    {
            foreach (Process clsProcess in Process.GetProcesses()) {
                    if (clsProcess.ProcessName.StartsWith(process))
                    {
                            //kill process if it is running
                            clsProcess.Kill();
                            //return true if process still running
                            return true;
                    }
            }
            //return false if process still not running
            return false;
    }
    Modding to find any:

    Code:
    public bool ProcessFinder(string process)
    {
            foreach (Process clsProcess in Process.GetProcesses()) {
                    if (clsProcess.ProcessName.StartsWith(process))
                    {
                            //return true if process still running
                            return true;
                    }
            }
            //return false if process still not running
            return false;
    }
    Bool functions, return true or false, you can use:

    Code:
    ProcessFinder("winamp"); [ aways without .exe ]
    
    ProcessFinderAndKiller("winamp"); [ aways without .exe ]
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. stayboogy

    stayboogy MDL Addicted

    May 1, 2011
    709
    115
    30
    @codyqx4, so far i've not had any trouble at all using your microsoft toolkit that uses vbc.exe to run kms. it's not disappeared ever, and both microsoft security essentials and avast even though they detect it when it runs as malicious they never notify the user exactly what the file/process is only the pid. but if vbc.exe is put on the exceptions/exclusions lists it never even notifies that anything is wrong at all.

    i don't think there is any need for you to make anything other than what you have honestly. the original kmsemulator, and this new kms run through vbc are perfect how they are. and even if something new you develop uses roughly no memory at all, it's still better to have something run once at startup and then stop as opposed to having something running all the time in the background. it's like de-evolution in the process i think...

    :cool:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,801
    44,854
    150
    This extra tool is for creating KMS hosts, not a replacement or change in the way anything else works. If is is expected to run 24/7, it needs to. Thing is I think vbc.exe dies on its own if it runs too long.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. stayboogy

    stayboogy MDL Addicted

    May 1, 2011
    709
    115
    30
    i see, sorry i misinterpreted what you asking above then :cool:

    keep up the good work :worthy:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...