views:

268

answers:

2

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();
                    }
                }
           }
+3  A: 

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.

peSHIr
Well. what I have mentioned is just an example, pls don't worry about the reason to close, IE when Gtalk is started. And I have implemented `Thread.Sleep(1000);` in my service. thanx for the response. I try to implement it +1.
infant programmer
Thread.Sleep is probably not the best route either, a timer will be a better bet
saret
+1  A: 

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

saret
Can you please mention a Link to guide the implementation of timer ..
infant programmer
sure here you go- http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
saret