Rowan
Rowan

Reputation: 57

How to make a countdown timer that only begins in certain programs

So before I begin, I would like you to know I already know how to make the timer. My only problem is making it only work when I am using certain programs. I am knew to programming and I want the timer to count back from 10,000 (you know, to do the ten thousand hour rule, yes I have already done a couple hundred but I would like to keep track of my progress). I want the timer to run on my desktop whenever I open my visual studio but I have no idea how to make this happen and I have had no luck on google with this so far, so any suggestions would really help!

Thanks in advance!

Upvotes: 1

Views: 253

Answers (3)

mont_
mont_

Reputation: 311

Is this what you wanted?

System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcessesByName("ProcessName");//Add visuals procname here
            if (proc.Length > 0)
            {
                MessageBox.Show("Process running");
                if (timer1.Enabled == false)
                {
                    timer1.Start();//Starts the countdown}
                    System.Threading.Thread.Sleep(1000);
                }
                else
                {
                    MessageBox.Show("Process not running");
                    if (timer1.Enabled == true)
                    {
                        timer1.Stop();//Stops the countdown}
                        System.Threading.Thread.Sleep(1000);
                    }

Upvotes: 1

jamesdgb
jamesdgb

Reputation: 55

You can just track if the process is opened as explained in this answer :How to Get Active Process Name in C#?

Upvotes: 1

mrtig
mrtig

Reputation: 2267

In your counter app you could set up a "process watcher" that checks this:

using System.Diagnostics;

Process[] procList = Process.GetProcesses();

foreach(Process p in procList ){
    if(p.ProcessName == "<visual studio process name>")
    { /*start the timer*/}
}

every minute, say. If it finds visual studio, then have it start your timer.

Upvotes: 1

Related Questions