Hello,

Im messing with processes for a project that Im doing, so if I detect that the specific process Im checking changes the title containing a specific word like "Hello" I will kill the process and start it again.

Im doing this with a timer so every X secs/minutes it checks for the title:

Process[] processPads = Process.GetProcessesByName("notepad");
foreach (Process processes in processPads)
{
    if (!processes.MainWindowTitle.Contains("Untitled"))
    {
        processes.Kill();
        Process.Start("notepad");
    }
}

That's what I have and works and everything but when I click anything on the menu like "File/Edit/Format/View/Help/etc" If I click any of those notepad gets killed and it gets started again, I displayed it before killing it so when I click anything on the menu the mainwindowtitle is empty "", Its there a way to get the form title of that specific process ? Example if I have 5 notepads opened and #3 changes name and doesnt contain "Untitled" I want #3 killed and started again not another notepad getting killed.

I m really curious about this one. I ve tested your code but declared processPads outside the the timer method. By doing that you don t get the problem and you can work with your notepads but you get another error wich is unusual.

Thats because when you don't put it inside the timer, it gets the title name before you clicked the menu, Put the "Process[] processPads = Process.GetProcessesByName("notepad");" outside the timer and make a new button and a listView.

private void button1_Click(object sender, EventArgs e)
{
    listView1.Items.Clear();
    foreach (Process processes in processPads)
    {
        listView1.Items.Add(processes.ProcessName + " " + processes.Id);   
    }
}

Open 1 notepad and click the button, it will tell you theres a notepad + its id, if you open a second notepad and you click the button it won't show it because processPads is not being "refresh" or giving new values to it.

I just don't get why the MainWindowTitle changes when you click menus/comboboxes/etc.

The thing is that when you click on any menu the property: MainWindowTitle goes into a empty string.Thats why the "Untitled" is being rejected and the code goes into the if and by that kills the proccess and starts over.

Try this:

 private void button1_Click(object sender, EventArgs e)
        {

            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            Process[] processPads = Process.GetProcessesByName("notepad");
            foreach (Process processes in processPads)
            {

                if (!(processes.MainWindowTitle.Contains("Untitled") || processes.MainWindowTitle==""))
                {
                    processes.Kill();
                    Process.Start("notepad");
                }
            }
        }

Yeah, I was thinking on doing that as a workaround, but I'll try to see if theres something else that can be done.

commented: if you do find something, post it here +2
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.