{"id":40,"date":"2009-07-07T10:55:40","date_gmt":"2009-07-07T15:55:40","guid":{"rendered":"http:\/\/blog.bitsofgenius.com\/?p=40"},"modified":"2009-07-19T16:59:01","modified_gmt":"2009-07-19T21:59:01","slug":"when-a-winform-can-be-automated-with-parameters","status":"publish","type":"post","link":"https:\/\/blog.bitsofgenius.com\/?p=40","title":{"rendered":"When a WinForm can be automated with parameters"},"content":{"rendered":"<p>Console applications fit nicely into the batch processing paradigm: take some parameters, do the work, write your progress and\/or notices to the console, report an exit code and terminate.\u00a0 Occasionally, doing the same work in a Windows form provides a better visual interface into the data and activity.<\/p>\n<p>A good example of this is the NT Backup Utility (Programs \/ Accessories \/ System).\u00a0 It can be run in interactive mode, so that the operator creates the configuration, or selects an existing one using the GUI, then launches the backup or restore project with a button.\u00a0 If that same application is started on the command line with parameters, the backup or restore window starts immediately&#8211;and automatically closes when complete.<\/p>\n<p>A Windows Form application in .NET can easily be modified to accept optional command line arguments.\u00a0 The sample project WinFromWithParameters (see the download page) is a Visual Studio 2008 solution demonstrating how to do it.\u00a0 The application will display enabled user controls when no parameters are provided, and wait for the operator to change the errorlevel selection and close the form, or press the terminate button.\u00a0 When parameters are provided, it will display the command-line parameters in the text box, but the textbox and the Test button are disabled.\u00a0 Instead, a timer is launched with a 5 second interval.\u00a0 The timer event will close the form and return a specific exit code to the caller.<\/p>\n<p>Things of interest in the code:<\/p>\n<ul>\n<li>\u00a0The key to making the WinForm see command-line arguments is to add the parameters <strong>string[] args<\/strong> to the static void main() declaration, and add the argument <strong>args<\/strong> to the Application.Run (Form1()); declaration  in Program.cs.\u00a0 In Form1.cs, the instantiation <strong>Form1()<\/strong> becomes <strong>Form1(string[] args)<\/strong>, and the form now has access to the command-line arguments.<\/li>\n<li>If command-line arguments are detected, the code uses a timer to trigger the processing.\u00a0 It is not a good idea to call your <strong>DoWork()<\/strong> method from the Form_Load event.\u00a0 Windows will continue to render the form and its controls, even after the Form_Load event is complete.\u00a0 The timer interval is usually set to some small amount (100 ms or so), and the first instruction within the timer event disables the timer from firing again.\u00a0 Upon entry into the timer event, the application has reached its idle state: when the <strong>DoWork()<\/strong> method should be executed.<\/li>\n<li>You can control the exit code using the <strong>System.Environment.ExitCode<\/strong> property.\u00a0 The <strong>System.Environment.Exit()<\/strong> method will set the exit code, and terminate your application (firing shutdown events) preventing lines after it in the method from executing.\u00a0 Using the property allows further code to execute, even allowing further overrides of the exit code.<\/li>\n<li>The key to executing the Win Form from a batch file, and read its resulting exit code, is to use the START command with the \/WAIT option. See the RunDemo.bat file in the project for an example.<\/li>\n<\/ul>\n<p>Some recommendations for automating WinForms:<\/p>\n<ul>\n<li>If you detect arguments, but they have invalid content, don&#8217;t inform the users via dialog box, etc.\u00a0 They probably won&#8217;t be there, and your process will hang.\u00a0 Log the error to an Event Log (Application, etc), and terminate with a specific exit code to semaphore the caller that your app failed.<\/li>\n<li>If you update controls on your form periodically, be sure to use the appropriate Application.DoEvents() or.Refresh() methods to ensure the form content is rendered.<\/li>\n<li>Sometimes, you have a Windows Form which does a function that you want to automate as part of a batch or other automated sequencing.\u00a0 If time is sparse, adding command-line parameters to the form can save a lot of refactoring into a new console app, or re-encapsulating functionality into different classes.<\/li>\n<\/ul>\n<p>WinFormWithParameters is available on the <a href=\"?page_id=8\">download<\/a> page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Console applications fit nicely into the batch processing paradigm: take some parameters, do the work, write your progress and\/or notices to the console, report an exit code and terminate.\u00a0 Occasionally, doing the same work in a Windows form provides a better visual interface into the data and activity. A good example of this is the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,23,17],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-dotnettechniques","category-dotnetwinforms","category-tips-and-tricks"],"_links":{"self":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=40"}],"version-history":[{"count":0,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts\/40\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}