4

I'm building a Windows Form application and I want to open "Microsoft Edge" through my app with a specific URL and wait until the user closes the Edge Window.

I tried it with this code:

using (Process p = Process.Start("microsoft-edge:www.mysite.com"))
{
    p.WaitForExit();
}

When I execute this code, Edge is launching with the correct URL ... but got a null object reference. The "p" object that I'm getting from Process.Start is null.

I think it's related to the reuse of Windows application.

Does anyone have a workaround/have an idea how I can wait for the user to close Edge?

Cellcon
  • 1,245
  • 2
  • 11
  • 27
Aviram Fireberger
  • 3,910
  • 5
  • 50
  • 69
  • Well, could could create a new Process instance, set the FileName and Arguments members of the StartInfo property, and call Start on it. But are you waiting for the user to close the site you opened, or exit the browser? – stuartd Nov 05 '15 at 14:43
  • you could look in the process list if your process still exists – online Thomas Nov 05 '15 at 14:45
  • What is supposed to happen when the user is done with your web content? Why should your application wait for it? Sounds like you're enroute to a "media discontinuity", forcing a user to jump between windows. Can't you display the web content in your application? – Alexander Nov 05 '15 at 14:53
  • 1
    Pretty much covered by [this Q+A](http://stackoverflow.com/questions/33042010/in-what-cases-does-the-process-start-method-return-false). There is no simple way to work around this, you can't tell when the user closes a tab to stop navigating the web page. – Hans Passant Nov 05 '15 at 16:54
  • Well I'm starting a proxy just before I open "Edge". It can't be done in an internal browser. I need specific the Edge browser to render and show the site. I'm closing the proxy once the user browser is closed. I need to know when the user exit the browser that was open through my app, it is posibile that the site will open other tabs as well. But I need to know when all tabs are closed (Edge application that was opened through got closed) – Aviram Fireberger Nov 07 '15 at 20:06
  • @stuartd - "Edge" browser can not be open through a FileName and Arguments (Only through this shell command). – Aviram Fireberger Nov 08 '15 at 07:43
  • @Thomas - how can I know which edge process is the one that I created? If the user already had one opened for example. – Aviram Fireberger Nov 08 '15 at 07:45
  • @Alexander - No , it must be rendered in "Edge" (please see last comment) – Aviram Fireberger Nov 08 '15 at 07:45
  • @Jumpy_Goat by pid check before, during and after – online Thomas Nov 08 '15 at 08:26
  • Why must it be Edge? – Alexander Nov 09 '15 at 09:50
  • @Alexander Does it really matter? (Customer Specs). Well now I did manage to wait, but for some reason "Edge" create for every tab a new process and when you close that tabs the process is'nt get exist. they are terminated only when all tabs/ the main browser is closed. So I do get what I want, But..... If the user had Edge opened before he will have to close all tabs to pass the "Process.Wait" blocker :\ Any ideas for this one? – Aviram Fireberger Nov 09 '15 at 10:08
  • To be honest, yes, it does matter. At least it would to me. Because it's a very fragile process you're making yourself dependent on. – Alexander Nov 09 '15 at 11:29
  • @Alexander Yeah I know, But that's the customer needs. Supports for IE, Edge and Chrome – Aviram Fireberger Nov 10 '15 at 08:12

2 Answers2

7

Finally I did managed to do so: When you launch Edge (at least) two process get created: MicrosoftEdge and MicrosoftEdgeCP.

MicrosoftEdgeCP - foreach tab. So we can "wait" on this new tab process that was just created.

//Edge process is "recycled", therefore no new process is returned.
Process.Start("microsoft-edge:www.mysite.com");

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

foreach (Process theprocess in edgeProcessList)
{
    if (newestEdgeProcess == null || theprocess.StartTime > newestEdgeProcess.StartTime)
    {
        newestEdgeProcess = theprocess;
    }
}

newestEdgeProcess.WaitForExit();
Cellcon
  • 1,245
  • 2
  • 11
  • 27
Aviram Fireberger
  • 3,910
  • 5
  • 50
  • 69
0

From an older solution - use a WebBrowser control to load the HTML. Once you get the data back, use an ObjectForScripting to call a c# method to notify when done.

See http://www.codeproject.com/Tips/130267/Call-a-C-Method-From-JavaScript-Hosted-in-a-WebBrowser