{"id":633,"date":"2004-09-22T21:18:51","date_gmt":"2004-09-23T01:18:51","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=633"},"modified":"2004-09-22T21:18:51","modified_gmt":"2004-09-23T01:18:51","slug":"the-using-keyword","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/09\/22\/the-using-keyword\/","title":{"rendered":"The &#8216;using&#8217; keyword"},"content":{"rendered":"<p>Joe pointed out that the <a href=\"http:\/\/cephas.net\/blog\/2004\/09\/03\/pgp_decryption_using_c.html\">C# PGP decryption code<\/a> that I wrote could be better; specifically I should be checking the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfsystemdiagnosticsprocessclassexitcodetopic.asp\">xExitCode property of the Process instance<\/a> and the code would also be better served if I made certain that I disposed of the Process instance by calling the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfsystemdiagnosticsprocessclassclosetopic.asp\">Close() method<\/a> when I&#8217;m done using it.  The ExitCode improvement is relatively simple to add; start the process, read any lines of output and then check the ExitCode to see if everything went smoothly:<br \/>\n<code><br \/>\nProcess process = Process.Start(psi);<br \/>\n...<br \/>\nwhile(!process.HasExited) {<br \/>\n\t... \/\/ do stuff<br \/>\n}<br \/>\nif (process.ExitCode != 0) {<br \/>\n\t\/\/ something went wrong..<br \/>\n}<br \/>\n<\/code><br \/>\nThe second thing that Joe mentioned was the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/csref\/html\/vclrfusingstatement.asp\">&#8216;using&#8217; statement<\/a>, which is a novel feature of C# that provides &#8220;&#8230; an easy way for C# code to acquire and release unmanaged or otherwise precious resources.&#8221;  The code I originally wrote didn&#8217;t destroy the handle to the process; after all was said and done I should have called the Close() or Dispose() method of the process:<br \/>\n<code><br \/>\nProcess process;<br \/>\ntry {<br \/>\n\tprocess = Process.Start(psi);<br \/>\n\t...<br \/>\n} catch {<br \/>\n\tprocess.Close();<br \/>\n}<br \/>\n<\/code><br \/>\nThe &#8216;using&#8217; statement is syntactic sugar that&#8217;s a shortcut for the well worn try \/ catch idiom and shortens the above example to:<br \/>\n<code><br \/>\nusing (Process process = Process.Start(psi)) {<br \/>\n\t... \/\/ do stuff w\/ the process<br \/>\n}<br \/>\n<\/code><br \/>\nwhich then automatically calls the Dispose() method of the process.<\/p>\n<p>Joe goes on to hijack the &#8216;using&#8217; statement in some other novel ways which should you <a href=\"http:\/\/blog.joecheng.com\/archives\/000025.html\">check out when you have a chance<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Joe pointed out that the C# PGP decryption code that I wrote could be better; specifically I should be checking the xExitCode property of the Process instance and the code would also be better served if I made certain that I disposed of the Process instance by calling the Close() method when I&#8217;m done using &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/09\/22\/the-using-keyword\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">The &#8216;using&#8217; keyword<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17,2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/633"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=633"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/633\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}