简体   繁体   中英

How to detect that a process is started using C# code[windows service]

I am trying to design a windows-service which monitors a process namely "Gtalk", if the process is started then, the browser Internet-explorer (process iexplore) should be closed. This should happen only when the process "Gtalk" is started, [not when running]

The code I have written and implemented, doesn't allow to open IExplore when Gtalk is running. That is certainly what not I am trying for. The process Gtalk should close browser only at its start-up, After the process is started, it should allow to open IExplore.

Is it possible with Windows service or is it must be the part of Gtalk process itself?

This is my code:

 while (true)
          {

                if (Process.GetProcessesByName("Gtalk").Length > 0)
                {

                    foreach (Process prc in Process.GetProcessesByName("IExplore"))
                    {
                        prc.Kill();
                    }
                }
           }

Without going into the "Why?" of this whole endeavour, to get what you seem to want you'd have to keep a list of "known" processes.

For instance, keep a list of process IDs you've seen for processes that have GTalk as the process name. Then, only if you see a process with a GTalk name appear that has a process ID number not in your list already, you add it to your list and perform the IExplore kill action.

Also, be sure to not be a processor hog, writing a tight endless loop in a Windows service like this. But this is not specifically related to your question.

From what you're saying (maybe I misread it), it's only at startup of GTalk that this should kill IE, thereafter IE can be launched. Can multiple instances of GTalk be running?

Do you control the GTalk code? Sounds like it would be best implemented in the GTalk startup procedure.

If not then @peSHIr's route of checking whether the GTalk process detected is one you've already seen might be your best bet.

However even if only one instance of GTalk can run, you would need to break out of the code that does the IE kill once first run (maybe storing the GTalk process ID), then periodically check (maybe utilizing a timer) if the GTalk process is running and if not (or if it is but with a different proccess ID) you would run the IE kill code again

Edit: simple example of Timer usage

Note that this timer object is Disposable, Here's a quick example;

//in startup code...
timer = new System.Threading.Timer(CheckProcessAndKillIeIfNeedBe, null, 0L, (long)TimeSpan.FromMinutes(1).TotalMilliseconds);

//some method elsewhere
public void CheckProccessAndKillIeIfNeedBe(object state)
{
  //do the process check and kill IE if conditions are met          
}

null = state, 0L means run now, and the last parameter is your interval in milliseconds for how often this should run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM