Home > Blockchain >  C# Close window when process is started
C# Close window when process is started

Time:05-17

i have a WPF window which should close when the process "TeamViewer" is started.

Unfortunately I can't manage to build a suitable loop. Can you help me?

 private void Window_Loaded(object sender, RoutedEventArgs e)
    {


       Process[] pname = Process.GetProcessesByName("TeamViewer");
       do
        {
            this.Close();
        }
       while (pname.Length > 0);

    }

Greetings iSteffen

CodePudding user response:

You should look for the process "TeamViewer" inside a loop, and on finding any instance of it, you can exit your form. Example code, this one watches for Notepad instance , you can modify it to your needs.

     do
     {
         Process[] proc = Process.GetProcessesByName("notepad");
         if (null != proc)
         {
              if (proc.Length > 0)
              {
                   //if any notepad process is running, then exit
                    return;
              }
         }

         //wait for some time, there are other efficient wait mechanisms
         Thread.Sleep(500); //wait for half sec

     }while(true);
  • Related