Home > Mobile >  Changing between Windows
Changing between Windows

Time:11-10

I'm trying to create a little tool that brings every 60 seconds eithe chrome, edge or "xyz" to the front. xyz is another programm i wrote.

The problem i have is, that it's bringing me the first programm (xyz) to the front but chrome or edge is never coming to the front. Also when i put chrome as first programm to show it's not coming.

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Threading;
namespace ChangeWindows
{
    class Program
    {
        static void Main(string[] args)
        {
            ShowWindow();
        }


        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        private static void ShowWindow()
        {
            // Bei start ein programm in den vordergrund holen
            // BDE
            Process[] processXYZ = Process.GetProcessesByName("xyz");
            IntPtr xyz = processXYZ[0].MainWindowHandle;
            SetForegroundWindow(xyz);
            // 1 min warten
            Thread.Sleep(60000);
            // nächstes Programm
            // msedge
            Process[] processEDGE = Process.GetProcessesByName("msedge");
            IntPtr edge = processEDGE[0].MainWindowHandle;
            SetForegroundWindow(msedge);
            // 1 min warten
            Thread.Sleep(60000);
            // nächstes Programm
            // chrome
            Process[] processCHROME = Process.GetProcessesByName("chrome");
            IntPtr chrome = processCHROME[0].MainWindowHandle;
            SetForegroundWindow(chrome);
            // 1 min warten
            Thread.Sleep(60000);
            // von vorne beginnen
            ShowWindow();

        }
    }
}

I'm also getting none error Message. For example like Couldn't find Process with name chrome not found.

CodePudding user response:

First, the process for Edge is called "msedge" not just "edge"

Second, as Jimi pointed out, you should search for the first process that it's MainWindowHandle is not 0x00000000

so it should look something like this

using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
namespace ChangeWindows
{
    class Program
    {
        static void Main(string[] args)
        {
            ShowWindow();
        }


        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        private static void ShowWindow()
        {
            // Bei start ein programm in den vordergrund holen
            // BDE
            Process processXYZ = Process.GetProcessesByName("xyz").FirstOrDefault(p => p.MainWindowHandle != IntPtr.Zero);
            if (processXYZ != null)
            {
                IntPtr xyz = processXYZ.MainWindowHandle;
                SetForegroundWindow(xyz);
                // 1 min warten
                Thread.Sleep(60000);
            }
            // nächstes Programm
            // msedge
            Process processEDGE = Process.GetProcessesByName("msedge").FirstOrDefault(p => p.MainWindowHandle != IntPtr.Zero);
            if (processEDGE != null)
            {
                IntPtr edge = processEDGE.MainWindowHandle;
                SetForegroundWindow(edge);
                // 1 min warten
                Thread.Sleep(60000);
            }
            // nächstes Programm
            // chrome
            Process processCHROME = Process.GetProcessesByName("chrome").FirstOrDefault(p => p.MainWindowHandle != IntPtr.Zero);
            if (processCHROME != null)
            {
                IntPtr chrome = processCHROME.MainWindowHandle;
                SetForegroundWindow(chrome);
                // 1 min warten
                Thread.Sleep(60000);
            }
            // von vorne beginnen
            ShowWindow();
        }
    }
}

Alternativeley, for the sake of simplicity, I made my own version based on your code, in case you want it, here it is:

using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;

namespace ChangeWindows
{
    internal class Program
    {
        private static readonly string[] processesNames = { "xyz", "msedge", "chrome" };

        static void Main(string[] args) => ShowWindow(60000);

        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        private static void ShowWindow(int sleep)
        {
            while (true)
            {
                foreach (var processName in processesNames)
                {
                    var process = Process.GetProcessesByName(processName).FirstOrDefault(p => p.MainWindowHandle != IntPtr.Zero);

                    if (process == null)
                        continue;

                    SetForegroundWindow(process.MainWindowHandle);
                    Thread.Sleep(sleep);
                }
            }
        }
    }
}
  • Related