none
How to Bring an application to front with C# RRS feed

  • Question

  • Hello:

    I have a C# Console Project using Puppeteer Sharp to grab some web pages. But from time to time, when I browse internet, web browsers are in front. As the web browsers hide Puppeteer Sharp window, then some actions, like the URL detect and click are affected, therefore, they don’t work from time to time, but if I keep the Puppeteer Sharp window (showing the Chromium page) in front, then usually, the Puppeteer Sharp program works.

    Therefore, I guess if I can write another program, can always bring the Puppeteer Sharp window to the front, then I don’t have to do this by hand.

    I searched around, and I got some code, like the following: (.Net Core Console Project with C# 7.3)

    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    
    namespace AppToFront
    {
        class Program
        {
            [DllImport("USER32.DLL")]
            public static extern bool SetForegroundWindow(IntPtr hWnd);
    
            static void Main()
            {
                //Checks if application is running
                try
                {
                    Process process = Process.GetProcessesByName("Chromium")[0];
                    if (process != null)
                    {
                        process.WaitForInputIdle();
                        IntPtr s = process.MainWindowHandle;
                        SetForegroundWindow(s);
                        Console.Write("Proccess found: " + process.ToString());
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ERROR: Application is not running!\nException: " + ex.Message);
                    Console.ReadKey();
                    return;
                }
            }
        }
    }
    

    But when I run the program.

    I always got the following result:

    ERROR: Application is not running!

    Exception: Index was outside the bounds of the array.

    C:\Program Files\dotnet\dotnet.exe (process 16720) exited with code -1.

    To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.

    Press any key to close this window . . .

    Even I can see from Task Manager that there are always at least 2 Chromium processes are running, but my program can’t detect any of them. But if I change the statement:

    Process process = Process.GetProcessesByName("Chromium")[0];

    To anther one, like this:

    Process process = Process.GetProcessesByName("Chrome")[0];

    I can always get the result if I open Google Chrome browser in my PC. But Chrome is not the window I want to bring to front. I want to get the Chromium window, but my code is not working.

    Any advice?

    By the way, as it is already 2019, are there any way to do the same job without using Windows API?

    My PC is running Windows 10 (Version 1903) and I am use Visual Studio 2019 (Version 16.1.6).

    Thanks,


    Sunday, July 21, 2019 8:48 PM

Answers

  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by zydjohn Sunday, September 8, 2019 5:03 PM
    Tuesday, July 30, 2019 8:55 AM
    Moderator

All replies

  • Hi zydjohn,

    Thank you for posting here.

    Based on your description, you want to bring an application to front in c#.

    I suggest that you don't use Chromium or Chrome browser because I find that SetForegroundWindow method is inapplicable for them based on my test.

    If you want to grad some web sites, I recommend that you could use IExplore to do it.

    Here is a code for your reference.

            [DllImport("user32.dll")]
            static extern bool SetForegroundWindow(IntPtr hWnd);
            static void Main()
            {
                Process process = Process.GetProcessesByName("iexplore")[0];
                BringProcessToFront(process);
            }
            public static void BringProcessToFront(Process process)
            {
                IntPtr handle = process.MainWindowHandle;
                SetForegroundWindow(handle);
            }

    Result:

    Note: If you use the SetForegroundWindow method, it is necessary for you not to minimize the window that will be brought to the front.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 22, 2019 6:08 AM
    Moderator
  • Hello:

    Thanks for your reply, but IE seems not an answer, as far as I know, Microsoft even wants to drop Edge and use Chrome like V8 JavaScript engine.  My current solution using Puppeteer Sharp can easily parse DOM element and click on the links, buttons I want.

    But to keep the UI window on Top is not so easy.  But why until now, there is no other ways to do this kind of job, and we can only use Windows API?

    Please advice if we can have other alternatives, like Nuget packages or others, without using Windows API?

    Thanks,

    Monday, July 22, 2019 7:31 AM
  • Hi zydjohn,

    Thanks for the feedback.

    I have updated my code, which could make it work for chrome.

    class Program
        {
            [DllImport("user32.dll")]
            static extern bool SetForegroundWindow(IntPtr hWnd);
            static void Main()
            {
                Process process = Process.GetProcessesByName("chrome")[0];
                process = Process.GetProcessById(process.Id);
                BringProcessToFront(process);
            }
            public static void BringProcessToFront(Process process)
            {
                IntPtr handle = process.MainWindowHandle;
                SetForegroundWindow(handle);
            }
        }

    Besides, If don't want to use windows API, you could ask it in the third-party forum.

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 22, 2019 9:08 AM
    Moderator
  • Hello:

    Thanks for your code, after my testing. Your code works for Chrome browser.  Just as my previous code.  However, please note: for Puppeteer Sharp, it is using Chromium, not Chrome.  I can see Chromium processes from Task Manager.  But if I change the code to look like this:

                Process process = Process.GetProcessesByName("Chromium")[0];

    I always got the following exception:

    System.IndexOutOfRangeException
      HResult=0x80131508
      Message=Index was outside the bounds of the array.
      Source=AppToFront
      StackTrace:
       at AppToFront.Program.Main() in C:\GitRepos\AppToFront\AppToFront\Program.cs:line 13

    It means the code can't find Chromium process.  

    Can you do some testing to find running Chromium processes, not the Chrome processes.

    Thanks,

    Monday, July 22, 2019 11:06 AM
  • Hi zydjohn,

    Thanks for the feedback.

    I have installed the chromium and tested it. Please note that Chromium in task manager it's name still is chrome, you could look at the following picture.

    Therefore, I suggest that you could use the following code. I have tested it, it works.

         Process process = Process.GetProcessesByName("chrome")[0];

    Besides, please close all the chrome browser windows when you test it.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 23, 2019 7:04 AM
    Moderator
  • Hello:

    I closed all the Chrome browser, but keep my Puppeteer Sharp program running.  From Task Manager, I can see the processes names are "Chromium", Puppeteer Sharp is not Chromium itself, but a wrapper of Chromium.  

    Besides, in my PC (Windows 10 Version 1903), the Chrome process name in Task Manager is called "Google Chrome", not just "Chrome".  Maybe you are using different OS?

    Please check the picture to understand my meaning.

    Thanks,

    Tuesday, July 23, 2019 8:12 AM
  • Hi zydjohn,

    Thanks for the feedback.

    The correct way to see process name is to see details instead of processes. You could look at the following picture.

    The "Chromium" just only a app name.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 23, 2019 8:28 AM
    Moderator
  • Hello:

    Thanks for your reply.  But I have other questions: since there are many Chrome processes, how I can find the Chrome process I want to bring to front.  For example, if I see the process in "Details", can I use both the process name (Chrome) and its location as shown in "Details" properties, for example, if I run my Puppeteer Sharp from "C:\Bin\....\Chrome..win".

    In short, I have to use at least 2 conditions together to pinpoint the specific process I want to bring to the front, since there are usually many many Chrome processes are running, using the first one in the processes list is not always correct.

    Thanks,

    Tuesday, July 23, 2019 1:28 PM
  • Hi

    Thanks for the feedback.

    If you want to find the Chrome process I want to bring to front, you could try to use the following code.

    [DllImport("user32.dll")]
            static extern bool SetForegroundWindow(IntPtr hWnd);
            static void Main()
            {
                Process[] process = Process.GetProcessesByName("chrome");
                foreach (var item in process)
                {
                    var p = Process.GetProcessById(item.Id);
                    BringProcessToFront(p);
                }
             
            }
            public static void BringProcessToFront(Process process)
            {
                IntPtr handle = process.MainWindowHandle;
                SetForegroundWindow(handle);
            }

    Note: SetForegroundWindow method only open your latest opened window.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 24, 2019 6:48 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by zydjohn Sunday, September 8, 2019 5:03 PM
    Tuesday, July 30, 2019 8:55 AM
    Moderator