I am creating a program that password protects software. The way I plan to protect it is to check if the program is running, and if it is, open up a window asking for a password. If it is wrong, close the protected software. I have this piece of code to check for the software process:

private void update_Tick(object sender, EventArgs e)
        {
            protecting.Text = "Protecting: " + Settings.Default.exe.ToString();
            if (protecting.Text == "Protecting: ")
            {
                protecting.Text = "Protecting: Nothing";
            }
            status.Text = Settings.Default.status.ToString();
            if (status.Text == "Disabled")
            {
                status.ForeColor = Color.Red;
            }
            else
            {
                status.ForeColor = Color.Green;
            }

            //Problem part
            IsProcessOpen("notepad.exe");
        }


        public bool IsProcessOpen(string name)
        {
            //here we're going to get a list of all running processes on
            //the computer
            foreach (Process clsProcess in Process.GetProcesses())
            {
                //now we're going to see if any of the running processes
                //match the currently running processes. Be sure to not
                //add the .exe to the name you provide, i.e: NOTEPAD,
                //not NOTEPAD.EXE or false is always returned even if
                //notepad is running.
                //Remember, if you have the process running more than once, 
                //say IE open 4 times the loop thr way it is now will close all 4,
                //if you want it to just close the first one it finds
                //then add a return; after the Kill
                if (clsProcess.ProcessName.Contains(name))
                {
                    //if the process is found to be running then we
                    //return a true

                    MessageBox.Show("You are not allowed to use this program...");
                    update.Enabled = false;

                    return true;
                }
            }
            update.Enabled = true;

            //otherwise we return a false
            return false;
        }
    }

But whenever I have that running and I start up notepad, it doesn't do anything. Oh yeah, and update is a timer that ticks ever 100 milliseconds. It's enabled too.

Dani AI

Generated

Short diagnosis and quick fixes (building on 's note): the Process API exposes the executable name without the extension or path (Process.ProcessName), so checking for "notepad.exe" will fail. Also avoid tight 100 ms polling — it wastes CPU and, if you show modal dialogs while polling, you can re-enter the check and get repeated dialogs.

A simple, reliable check is to ask for processes by base name and handle them gracefully. For example:

var procs = Process.GetProcessesByName("notepad");
foreach (Process p in procs)
{
    // try a graceful close first, then force if needed
    if (p.CloseMainWindow())
    {
        if (!p.WaitForExit(3000))
            p.Kill();
    }
    else
    {
        p.Kill();
    }
}

Practical timer and UI tips: stop the timer before showing any modal UI (call update.Stop() or update.Enabled = false) so the tick handler cannot re-run while the dialog is up; re-enable when done. Use a much gentler polling interval (1s or more) or, better, switch to an event-driven watcher (ManagementEventWatcher for Win32_ProcessStartTrace) so you only react when a process actually starts.

Design and security notes: externally killing processes is brittle and not a secure protection. A stronger approach is a small launcher that authenticates and then starts the protected exe as a child process (so you control lifecycle), or integrate authentication inside the protected app. Also remember you may need elevated rights to terminate some processes, and forcibly killing can cause data loss — prefer CloseMainWindow() then Kill() as a fallback.

Recommended Answers

All 3 Replies

first. 100 ms is way to frequent. you could save some processor usage by slowing that down to 1200ms.

2nd IsProcessOpen("notepad.exe"); according the comment in your IsProcessOpen method, it says not to use the ".exe" but instead use just "notepad"

//Be sure to not
//add the .exe to the name you provide, i.e: NOTEPAD,
//not NOTEPAD.EXE or false is always returned even if
//notepad is running.

Also you don't seem to do anything with the information after you find it.

Yeah, I tested it. It works fine. just don't put .exe on the process name.

IsProcessOpen("notepad");

another note. you might want to disable the update timer before you call the message box. otherwise you are going to end up with 10 message boxes before you get a chance to click ok.

Wow. Ishould probably stop programming late at night. Thanks guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.