简体   繁体   中英

Initial Form of C# program unavailable while the program is running

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.

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?

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

Exception of type 'System.OutOfMemoryException' was thrown.

on the line aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent) ;

The code is below:

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();
}

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 ( Form1 ) so the below code may not be needed. The timer above calls OnTimedEvent which is below.

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

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#.

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?

BTW, I asked a completely unrelated question about this project here

Any ideas?

You should simply remove the while loop

    System.Timers.Timer aTimer;
    // Create a timer with a one second interval.
    aTimer = new System.Timers.Timer(1000);
    // 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();

Timer runs on a separate Thread when you start it. Your while loop just keeps starting the timer over and over again.

System.Timers.Timer

To do this I have an infinite while loop that calls the timer. Is this the correct way to do this?

No. The while loop blocks the UI thread, which in turn makes the program freeze. There's no reason to have the while loop as you're already using a timer to trigger the event.

Without seeing your full, actual, code it's hard to say, but the tradtional method of doing a long-running process without locking the UI is to use Threading in C#.

Can you do your long running action in its own thread so that you don't lock up the UI thread?

Here's a tutorial: http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx

Don't use a while loop. If you want to run your OnTimedEvent method once a second, use something more like this:

Timer myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
myTimer.Interval = 1000; // 1000 ms is one second
myTimer.Enabled = true;
myTimer.Start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM