Initial Form of C# program unavailable while the program is running
<p>I am having a problem getting the main Form in my program to run as I want. I am using C#.  On my initial main form (Form1) I have a command button that runs a long program.  In the middle of the program I want the user to be able go back to the initial form and click on some new checkboxes that I will place on that initial form from within the C# program.  The code below just freezes the initial form.  I do not need to get the code below to work exactly.  I just need it to allow me to access the main initial form in the middle of the program that I started with the command button.</p>

<p>To do this I have an infinite while loop that calls the timer.  Is this the correct way to do this?  I do not need the program below to work.  I just need to be able to access that initial Form in the middle of the program. Since it is not working it seems that it is not the way to do this but what is the correct way?</p>

<p>The following code runs the OnTimedEvent function (method) below.  The function used to put up a Messagebox but I commented that out.   I do NOT need that function to work.  It is only there for testing purposes.  My goal is that I need the initial main Form to allow me to enter more information while it is running from the command button.  The function below runs about 15 times and I get the error</p>

<blockquote>
  <p>Exception of type 'System.OutOfMemoryException' was thrown.</p>
</blockquote>

<p>on the line <code>aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent)</code>;  </p>

<p>The code is below:</p>

<pre><code>System.Timers.Timer aTimer;
// Create a timer with a one second interval.
aTimer = new System.Timers.Timer(100);

while (true)
{
    // Hook up the event handler for the Elapsed event.
    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

    // Only raise the event the first time Interval elapses.
    aTimer.AutoReset = false;
    aTimer.Enabled = true;  // uncommented this now
    //addded  below
    aTimer.Start();
}
</code></pre>

<p>I have tried it in several different ways but the main Form always just freezes.  I just want to be able to select things (checkboxes, for instance) on the main Form (<code>Form1</code>) so the below code may not be needed.  The timer above calls <code>OnTimedEvent</code> which is below.</p>

<pre><code>private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
  // MessageBox.Show("Hello World");
}
</code></pre>

<p>In many places on the web I have seen (including stackoverflow) that I should be using timers to get the main initial Form to be useable but the code above just causes the Form to be Frozen and I get the bar at the top of the form indicating that it is Not Responding.
I am using Windows XP and Visual Studio 2008.  As I indicated above I am using C#.</p>

<p>To summarize, is the above  code the correct way to get the main, Initial form to be available after a command button has been running?  If it is, what am I doing wrong?  If this is not the correct way, what is?</p>

<p>BTW, I asked a completely unrelated question about this project <a href="http://stackoverflow.com/questions/14589943/reading-and-writing-from-a-serial-port-in-c-sharp-part-2">here</a> </p>

<p>Any ideas?</p>
