1

I'm trying to kill a process, but I can't bind a value to the process variable. This is what I got for my code;

URL = (string)result[1];
Process browser = Process.Start(@"chrome.exe","http:\\www.youtube.com/watch?v=" + URL);
Audio.SetApplicationVolume(APP, 15);

resultaat = (string)result[2];
Videolengte = Convert.ToInt32(resultaat);
Tijd = Videolengte;
Tijd = 10;
System.Threading.Thread.Sleep(Tijd);
browser = GetWindowProcess((string)result[3]);
//wanneer timer van het nummer weer klaar is, volgende dingen uitvoeren.
Audio.SetApplicationVolume(APP, 15);
browser.Kill();
deleteRow();

GetWindowProcess

 private Process GetWindowProcess(string windowTitle)
    {
        foreach (var proc in Process.GetProcessesByName("chrome"))
        {
            if (proc.MainWindowTitle.IndexOf(windowTitle, StringComparison.OrdinalIgnoreCase) > 0)
            {
                return proc;
            }
        }

        return null;
    }

It says browser is null when I try to kill it while I binded a processname to it, does anyone know the cause of this?

Obsidian Phoenix
  • 4,083
  • 1
  • 22
  • 60
joostmakaay
  • 437
  • 2
  • 7
  • 14

1 Answers1

0

It looks like your problem is inside GetWindowProcess. When you perform proc.MainWindowTitle, it's returning an empty string for each process - Probably because the Chrome windows don't have titles, and the Tabs are not considered "windows"

Take a look at this answer to the same question elsewhere. It looks like it's been this way for a good few months at least...

https://stackoverflow.com/a/16958798/2505574

If, according to your comments on this answer (which isn't what you are saying in your question), you want to kill all chrome processes, then you just need to write a method that iterates the processes and kills them:

public void KillChromeProcesses()
{
  foreach (var proc in Process.GetProcessByName("chrome"))
  {
    proc.Kill();
  }
}
Community
  • 1
  • 1
Obsidian Phoenix
  • 4,083
  • 1
  • 22
  • 60