-1

I am built same win Form that opens some website with webBrowser after this i need to run runMyfunction,but this doesn't happens.

Then i run runMyfunction, all stops and when i debug it it stays on

Application.Run(new Form1());

What i am missing here? This my code :

namespace UmbWin
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            //I tried this also
            //Form1 myForm = new Form1();
            //myForm.Show();
            //myForm.runMyfunction()- doesn't do anything

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }  
    }
}
public Form1()
{
    InitializeComponent();
}
[STAThread]
private void Form1_Load(object sender, EventArgs e)
{
    string authHeader = "User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)";
    webBrowser.Navigate("https://www.test.com/Default.aspx", null, null, authHeader);

    runMyfunction();
}      
public void runMyfunction()
{
    //here where it stops and does nothing
    HtmlElement head = webBrowser.Document.GetElementById("123");
    HtmlElement scriptEl = webBrowser.Document.CreateElement("script");
}
J3soon
  • 3,013
  • 2
  • 29
  • 49
Vladimir Potapov
  • 2,347
  • 7
  • 44
  • 71
  • Why don't you use *Form_Closed*(Form_Closing) event : https://msdn.microsoft.com/en-us/library/system.windows.forms.form.closed%28v=vs.110%29.aspx – Fabjan Dec 28 '15 at 11:01
  • I need to run this function while the webBrowser is running ,not when i close the app – Vladimir Potapov Dec 28 '15 at 11:02
  • Aside from your current problem (by the way "all stops" is not a very descriptive definition) - it looks like you have to move your call to `runMyFunction` into `DocumentCompleted` event handler of webbrowser. Otherwise it can be run before document in webbrowser will be completely loaded. – Andrey Korneyev Dec 28 '15 at 11:03
  • `// here it stops and does nothing`: looks like exception happens when form is loading and there is a known issue of such cases: http://stackoverflow.com/questions/4933958/vs2010-does-not-show-unhandled-exception-message-in-a-winforms-application-on-a – ASh Dec 28 '15 at 11:10

1 Answers1

4

That is because Application.Run will only complete once the main form of the application is closed.

Application.Run runs the message loop (it checks for clicks and other Windows events. Read up on how Windows / Win32 works if you don't know what this all means). It is meant to run forever.

Your runMyfunction will only run once after the Navigate call. That's all. If you want to run it after every navigate, subscribe to the DocumentCompleted event.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325