1

my problem is following:

I am currently opening up Microsoft Edge through .NET code to be able to read PFD-Files. But I only want to open Edge with a PDF-File, if it's not already opened.

The problem with Edge is, that the window is hosted by the ApplicationFrameHost, which also hosts other Windows Apps like Minesweaper from the store. So when I am opening e.g. Minesweaper after opening my pdf file with Edge, the current ApplicationFrameHost MainWindowTitle is "Minesweaper".

But if I start my code, it should check at the beginning if Edge is already opened, but I can't check it through the ApplicationFrameHost MainWindowTitle, because it is from the current active ApplicationFrameHost, which is "Minesweaper", because I it was the last active ApplicationFrameHost window.

I also can't check if the MicrosoftEdgeCP process is running, because it is always running, even if I close Microsoft Edge.

Do you have any solutions for my problem?

TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

0

When you launch Edge (at least) two process get created: MicrosoftEdge and MicrosoftEdgeCP.

You can try to use the following code to check whether the Edge browser is opened:

//We need to find the most recent MicrosoftEdgeCP process that is active
Process[] EdgeCPProcessList = Process.GetProcessesByName("MicrosoftEdgeCP");

Process newestEdgeCPProcess = null;

foreach (Process theprocess in EdgeCPProcessList)
{
    if (newestEdgeCPProcess == null || theprocess.StartTime > newestEdgeCPProcess.StartTime)
    {
        newestEdgeCPProcess = theprocess;
        Console.WriteLine("EdgeCP Process: "+theprocess.ProcessName.ToString());
    }
}


Process[] edgeProcessList = Process.GetProcessesByName("MicrosoftEdge");
Process newestEdgeProcess = null;

foreach (Process edgeProcess in edgeProcessList)
{
    if (newestEdgeProcess == null || edgeProcess.StartTime > newestEdgeProcess.StartTime)
    {
        newestEdgeProcess = edgeProcess;
        Console.WriteLine("Edge Process: " + edgeProcess.ProcessName.ToString());
    }
}

Console.ReadKey();

If we could get the ProcessName, means the Edge is already opened. The above code, works well on my side.

Zhi Lv
  • 18,845
  • 1
  • 19
  • 30
  • The problem with checking with this is, that even if you close Microsoft Edge, these two processes will continue running. I checked it with a freshly turned on PC. There are no such processes. But when I start Edge and close it again, the processes are still there in the background. So this will work the first time Edge is opened at that day, but not if it was already opened. – SomeRandomGuy Mar 07 '19 at 07:56
  • What's your Windows OS environment? I use Windows 10 Enterprise OS, on my PC, if I close Edge, these processes will be closed. So, it seems that your Edge browser is not being completely closed. Here is [a thread about edge browser seems to be not completely closing](https://answers.microsoft.com/en-us/edge/forum/all/edge-browser-seems-to-be-not-completely-closing/a5bed50c-b258-403b-8dfe-cb555631f86c?page=2), you can refer to it. Also, you can try to [reset the Edge browser](https://www.thewindowsclub.com/reset-microsoft-edge-browser-to-default-settings-in-windows-10). – Zhi Lv Mar 07 '19 at 09:58
  • I will take a look at it over the weekend and tell you about my results. – SomeRandomGuy Mar 08 '19 at 11:18