Home > Back-end >  Is there a way to block a Programm with a Windows Service
Is there a way to block a Programm with a Windows Service

Time:05-11

Im currently sitting here trying to writte a Windows Service for a Friend of mine who cant concentrate in Home Office and tempt to Game and Watching Videos. So he asked me if I could writte him a Programm which should block some of these Programms until he finished his work. I looked for a good way to realise this and I came to the conclusion that a Windows Service would be great. Runs in the Background, Executes Stuff, dont need much Computing Power, etc. Now my Problem is, there any way to do this? Or is it just not possible?

CodePudding user response:

You can create a window service that will check at a periodic interval if a process is running or not, and if it is running, you can kill a process by its name.

var process in Process.GetProcessesByName("processname");
if(process != null) 
   process.Kill();

CodePudding user response:

Thanks for your fast help. It works just fine now. I had to make a few changes on your lines since the GetProcessByName Method returns an Array. I now have it running like followed.

   Process[] process = Process.GetProcessesByName("notepad");
        int numberOfProcesses = process.Length;
        for (int i = 0; i < numberOfProcesses; i  )
            if (process[i] != null)
                process[i].Kill();

I now just make the if condition a time sensitive one like read system time compare it and then decide if you have to cancel it. Thanks.

  • Related