{"id":10243,"date":"2011-07-06T07:00:00","date_gmt":"2011-07-06T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/07\/06\/looking-at-the-problem-at-the-wrong-level-closing-a-processs-stdin\/"},"modified":"2011-07-06T07:00:00","modified_gmt":"2011-07-06T07:00:00","slug":"looking-at-the-problem-at-the-wrong-level-closing-a-processs-stdin","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110706-00\/?p=10243","title":{"rendered":"Looking at the problem at the wrong level: Closing a process&#039;s stdin"},"content":{"rendered":"<p>\nA customer was having trouble manipulating the stdin stream\nthat was given to a process.\n<\/p>\n<blockquote CLASS=\"q\">\n<p>\nHow do you simulate sending Ctrl+Z to a hidden console process\nprogrammatically?\n<\/p>\n<p>\nI am using <code>Redirect&shy;Standard&shy;Input<\/code>\nand want to send the console a Ctrl+Z.\nI&#8217;ve tried sending ASCII code 26, but that doesn&#8217;t work.\n<code>Generate&shy;Console&shy;Ctrl&shy;Event<\/code> supports Ctrl+C\nand\nCtrl+Break but not Ctrl+Z.\n<\/p>\n<p>\nHere&#8217;s what I&#8217;m doing, but it doesn&#8217;t work:\n<\/p>\n<pre>\nProcessStartInfo info = new ProcessStartInfo(@\"...\");\ninfo.CreateNoWindow = true;\ninfo.RedirectStandardError = true;\ninfo.RedirectStandardOutput = true;\ninfo.RedirectStandardInput = true;\ninfo.UseShellExecute = false;\nProcess p = Process.Start(info);\n\/\/ 0x1A is ASCII code of Ctrl+Z but it does not work\np.StandardInput.WriteLine(\"\\x1A\");\n<\/pre>\n<\/blockquote>\n<p>\nThe customer was kind enough to do more than simply ask the\nquestion.\nThe customer set up the scenario and even provided a code fragment\nthat illustrates the problem.\nWhich is good, because the original question <i>was the wrong question<\/i>.\n<\/p>\n<p>\nThe customer asked about simulating typing Ctrl+Z to a console,\nbut what they actually doing was sending a character to stdin;\nthey weren&#8217;t sending it to a console.\nIn fact, the way they created the process,\n<i>there is no console at all<\/i>.\n<\/p>\n<p>\nThe customer confused stdin with consoles.\nIt&#8217;s true that Ctrl+Z is the convention used by console windows\nto indicate that stdin should be closed.\nBut that is hardly any consolation when you took control of stdin yourself\nand are not using a console window to manage it.\n<\/p>\n<p>\nIt&#8217;s like saying,\n&#8220;Normally, when I want somebody to take my order,\nI pull into a parking space and\n<a HREf=\"http:\/\/www.seattlepi.com\/default\/article\/Cheap-Eats-Burgermaster-1163526.php\">\nturn on my headlights<\/a>,\nand somebody will come out.\nBut I can&#8217;t get it to work.&#8221;\n<\/p>\n<p>\n&mdash; <i>Um, that&#8217;s because you pulled into your own driveway.<\/i>\n<\/p>\n<p>\nCtrl+Z is a convention used by console windows to indicate that\nstdin should be closed, but if you said &#8220;I am going to manage stdin myself,&#8221;\nthen you aren&#8217;t using a console window,\nand that convention carries no weight.\nIf you write a Ctrl+Z to the process&#8217;s stdin, it will simply read a Ctrl+Z.\nBut since you are managing stdin yourself,\nyou can do it yourself:\nJust take the stream you set as the process&#8217;s stdin and\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.diagnostics.processstartinfo.redirectstandardinput.aspx\">\nclose it<\/a>.\n<\/p>\n<p>\n<b>Exercise<\/b>:\nPerhaps you can answer this related question from a different customer:\n<\/p>\n<blockquote CLASS=\"q\">\n<p>\nI am trying to send a Ctrl+C (SIGINT) to a process.\n<\/p>\n<pre>\nCurrentProcess = new Process();\nCurrentProcess.StartInfo.FileName = \"foo.exe\";\nCurrentProcess.StartInfo.UseShellExecute = false;\nCurrentProcess.StartInfo.RedirectStandardInput = true;\nStandardInputWriter = CurrentProcess.StandardInput;\nchar c = '\\u0003';\nStandardInputWriter.Write(c);\nStandardInputWriter.Flush();\nStandardInputWriter.Close();\n<\/pre>\n<p>\nIf I launch the process from a command prompt and type\nCtrl+C, it flushes its output and terminates,\nbut when I start it from within my application and send\nit a Ctrl+C via the code above,\nthe process is still running.\nHow do I send a Ctrl+C to a process?\n<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>A customer was having trouble manipulating the stdin stream that was given to a process. How do you simulate sending Ctrl+Z to a hidden console process programmatically? I am using Redirect&shy;Standard&shy;Input and want to send the console a Ctrl+Z. I&#8217;ve tried sending ASCII code 26, but that doesn&#8217;t work. Generate&shy;Console&shy;Ctrl&shy;Event supports Ctrl+C and Ctrl+Break but [&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-10243","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer was having trouble manipulating the stdin stream that was given to a process. How do you simulate sending Ctrl+Z to a hidden console process programmatically? I am using Redirect&shy;Standard&shy;Input and want to send the console a Ctrl+Z. I&#8217;ve tried sending ASCII code 26, but that doesn&#8217;t work. Generate&shy;Console&shy;Ctrl&shy;Event supports Ctrl+C and Ctrl+Break but [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10243","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=10243"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10243\/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=10243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=10243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=10243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}