{"id":2883,"date":"2013-10-21T07:00:00","date_gmt":"2013-10-21T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/10\/21\/opening-and-manipulating-internet-explorer-windows-programmatically\/"},"modified":"2013-10-21T07:00:00","modified_gmt":"2013-10-21T07:00:00","slug":"opening-and-manipulating-internet-explorer-windows-programmatically","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20131021-00\/?p=2883","title":{"rendered":"Opening and manipulating Internet Explorer windows programmatically"},"content":{"rendered":"<p>\nToday&#8217;s Little Program takes\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/11\/18\/10238335.aspx\">\nthe JavaScript application from a few years ago<\/a>\nand converts it to C#.\nThis was inspired by a customer who started with the question,\n&#8220;How can I close all Internet Explorer windows programmatically?&#8221;\n<\/p>\n<p>\nThis was a strange request.\nAfter all, the user may be rather upset that their Amazon shopping\nspree was suddenly terminated mid-stream.\n<\/p>\n<p>\nUpon closer questioning, the customer explained that they had a\nbusiness process\nwhich, among other things, opened a bunch of Internet Explorer\nwindows,\nand when the operation was over, they wanted to close the windows.\n<\/p>\n<p>\nOkay, so they didn&#8217;t really want to close <i>all<\/i> Internet Explorer\nwindows.\nThey just wanted to close the ones that they had opened.\n<\/p>\n<p>\nWhich is easy to do.\nIn fact, it&#8217;s a line-by-line translation of the JavaScript version!\n<\/p>\n<pre>\nclass Program {\n public static void Main() {\n   var ie = new SHDocVw.InternetExplorer();\n   ie.Visible = true;\n   ie.Navigate(\"http:\/\/www.microsoft.com\/\");\n   System.Threading.Thread.Sleep(5000);\n   ie.Quit();\n }\n}\n<\/pre>\n<p>\nIf you want to open a dozen browser windows and then close them later,\nthen go ahead and open a dozen browser windows,\nand then close them when you&#8217;re done.\n<\/p>\n<pre>\nclass Program {\n public static void Main() {\n  var windows = new System.Collections.Generic.\n                    List&lt;SHDocVw.InternetExplorer&gt;();\n  for (int i = 0; i &lt; 12; i++) {\n   var ie = new SHDocVw.InternetExplorer();\n   windows.Add(ie);\n   ie.Visible = true;\n   ie.Navigate(\"http:\/\/www.microsoft.com\/\");\n  }\n  \/\/ blah blah wait for user to finish business process\n  \/\/ (We'll just sleep for a few seconds.)\n  System.Threading.Thread.Sleep(10000);\n  \/\/ Close all the windows\n  foreach (var ie in windows) {\n   ie.Quit();\n  }\n }\n}\n<\/pre>\n<p>\nActually, let&#8217;s make it a bit more interesting.\nSuppose the intranet site sends you to the page\n<code>http:\/\/contoso\/finished.aspx<\/code>\nwhen you finish submitting your Widget Exemption Request.\nWe want to close the Internet Explorer window\nwhen that happens.\n<\/p>\n<pre>\nclass Program {\n static void ProcessWidgetExemptionRequest() {\n  var exit = new System.Threading.EventWaitHandle(\n             false, System.Threading.EventResetMode.ManualReset);\n  var ie = new SHDocVw.InternetExplorer();\n  ie.Visible = true;\n  ie.Navigate(\"http:\/\/contoso\/start.aspx\");\n  ie.DocumentComplete += (object frame, ref object url) =&gt; {\n   if (url as string == \"http:\/\/contoso\/finished.aspx\") {\n    exit.Set();\n   }\n  };\n  \/\/ Wait for the user to finish submitting their Widget\n  \/\/ Exemption Request.\n  exit.WaitOne();\n  \/\/ Clean up the IE window\n  ie.Quit();\n }\n public static void Main() {\n {\n  ProcessWidgetExemptionRequest();\n }\n}\n<\/pre>\n<p>\nThe <code>Process&shy;Widget&shy;Exemption&shy;Request<\/code>\nmethod creates an event that we will use to tell us when the user has\nfinished the operation.\nWe then create an Internet Explorer window,\nnavigate it to the Exemption Request Web site,\nand hook up an anonymous function that listens\nfor document completion events.\nThen we wait.\n<\/p>\n<p>\nWhen we get a completion event that says that the user has\nreached the Finished page,\nwe set our private event,\nwhich causes the wait to complete,\nat which point we clean up the Internet Explorer window\nand return to our caller.\n<\/p>\n<p>\nYou can try out this program yourself,\nbut you probably want to change the URLs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Little Program takes the JavaScript application from a few years ago and converts it to C#. This was inspired by a customer who started with the question, &#8220;How can I close all Internet Explorer windows programmatically?&#8221; This was a strange request. After all, the user may be rather upset that their Amazon shopping spree [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-2883","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Today&#8217;s Little Program takes the JavaScript application from a few years ago and converts it to C#. This was inspired by a customer who started with the question, &#8220;How can I close all Internet Explorer windows programmatically?&#8221; This was a strange request. After all, the user may be rather upset that their Amazon shopping spree [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2883","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=2883"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2883\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=2883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=2883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=2883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}