Calling method when process starts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Saser
    New Member
    • Jul 2009
    • 17

    Calling method when process starts

    Hi all bytes out there.

    I have a few little problems in my program.

    The program is supposed to help me to not spend too much time of my day at the computer ;)

    I play a game called Counter-Strike (CS) a lot, but I'm limited to 2 ½ hours a day, and a total of 4 hours a day. I have one button in my program which starts CS, and one button that "kills" CS. The Start-button starts a timer, and the Stop-button stops it. But I want to start the timer even if the game is started from somewhere else, like from the program Steam which you play CS from. In Steam you can double-click at a game, and the game starts. How do I call the method for starting the timer when the process for the game (hl.exe) starts?

    I also want to stop the timer when the game quits.

    Regards,
    Saser

    EDIT: Solved the first problem, but I think it's not a very good solution, have a look for yourself:
    Code:
    Process[] cs = System.Diagnostics.Process.GetProcessesByName("hl");
                foreach (Process p in cs)
                {
                    csRem.Set(csRemH, csRemM, csRemS);
                    csTimer.Start();
                }
    This is called everytime the timer totTimer ticks (interval 1000 ms). csRem is a time, csRemH, csRemM and csRemS is ints that tell how much time I have left in CS, csTimer is the timer for my CS-time.

    EDIT2: Tried to set csProcess.Enabl eRaisingEvents = true, but when I start CS from the button in my program, the timer doesn't start. Weird.
    EDIT2.1: Oh sorry, it did start the timer, but it wont call the method for stopping the timer when CS quits.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I don't personally know how to write a process monitor tool, but I did a google search and there should be some support out there if nobody here can help you.

    Actually, one link I found, which sounds like it might be right up your alley is:


    I will say though, be careful with these tools as STEAM does do it's best to monitor background processes which interact with its programs in an attempt to detect cheating, though as long as you're not hooking into the executables you should be fine. The code in that link just monitors the currently running processes so that shouldn't be anything "bad" so far as STEAM is concerned.

    At this point I'd say you could probably save yourself a lot of time and effort with an alarm or two and some willpower, and that you'll likely actually spend more time on the computer developing this tool than you would just playing CS anyways, but my inner-geek totally understands where you're coming from.

    Best of luck!

    Comment

    • Saser
      New Member
      • Jul 2009
      • 17

      #3
      Thanks! I'll see if this code helps me :)

      I started creating this program for two main reasons:
      1. To have control over my time at the computer.
      2. The programmming challenge.

      You're right, I've been spending a little too much time at my computer since I started this project, but look at it from this point: instead of playing a game, I've been creative, and developed something that helps me :)

      I'm soon going to "release" this project to my friends, if they want to, but to be able to release it to the people that doesn't know C# or any programming at all I've got to provide some kind of Settings-dialog. So in the end I think my little app will help, not only me, but other people as well. :)

      Thanks,
      Saser

      Comment

      • kaioshin00
        New Member
        • Nov 2006
        • 46

        #4
        Did you try registering to the Process.Exited event?

        Comment

        Working...