wpf - C# hang and stuck after Application.Run() at for loop -


im building program surf several websites , something.

after surfing 5 urls successfully, program hangs after application.run() line. program doesn't enter handler function , stuck. cpu usage 0 @ point.

i tried closing threads in possible way. i'm doing wrong?

i'm doing that:

[stathread] private static void main(string[] args)  {     (int = 0; < urls.count; i++)      {         var th = new thread(() = >          {             var webrowser = new webbrowser();             webrowser.allownavigation = true;             webrowser.documentcompleted += handler;             webrowser.navigate(urls[i]);             application.run();         });         th.setapartmentstate(apartmentstate.sta);         th.start();         th.join();     } } 

and handle function is:

private static void handler(object sender, webbrowserdocumentcompletedeventargs e)  {     webbrowser webrowser = sender webbrowser;     var htmldocument = webrowser.document;      /*do something*/      application.exit();     application.exitthread();      webrowser.dispose();     webrowser.stop();      thread.currentthread.abort(); } 

my problem similar one: application.run() leads application hanging

there no answer in question either.

thanks!

i think doing several mistakes:

  • you joining inside look
  • you calling application.exit() in each handler call

you should move joining outside loop , not call application.exit.

the following sample seems work well:

static class program {   [stathread]   static void main()   {      var urls = new list<string>() {          "http://stackoverflow.com",         "http://stackoverflow.com",         "http://stackoverflow.com",         "http://stackoverflow.com",         "http://stackoverflow.com",         "http://stackoverflow.com",         "http://stackoverflow.com",         "http://stackoverflow.com"};       var threads = new thread[urls.count];       (int = 0; < urls.count; i++)      {         threads[i] = new thread((url) =>         {            var webrowser = new webbrowser();            webrowser.allownavigation = true;            webrowser.documentcompleted += handler;            webrowser.navigate(url string);            application.run();         });         threads[i].setapartmentstate(apartmentstate.sta);         threads[i].start(urls[i]);      }       foreach (var t in threads)         t.join();       application.enablevisualstyles();      application.setcompatibletextrenderingdefault(false);      application.run(new form1());   }    private static void handler(object sender, webbrowserdocumentcompletedeventargs e)   {      webbrowser webrowser = sender webbrowser;       var htmldocument = webrowser.document;       /*do something*/       application.exitthread();       webrowser.dispose();      webrowser.stop();   } } 

Comments