DevOragz
DevOragz

Reputation: 131

Win form app freezing upon launch

Let me first confess that I am a fairly green programmer but I am in dire straits trying to figure out what is wrong with my application.

The goal so far is to make a timer kick off when the button is clicked and the elapsed time continually display on the text box.

There are probably better ways to implement this but humor me for a second and I practice creating events and using them in programs.

What I see happening when I launch the code is that it just freezes and never recovers, I need to end the app with the task manager.

Any pointers on what I may be doing wrong and how to fix it will be appreciated.

// see clock class below containing delegate and event instantiation

public class Clock
{
    public delegate void TimeChangedHandler(object clock, TimeEventArgs timeInfo);
    public TimeChangedHandler TimeChanged;

    public void RunClock()
    {
        TimeEventArgs e = new TimeEventArgs();//initialize args
        while (e.keepCounting)
        {
            Thread.Sleep(1000);
            e.EndTime = DateTime.Now;

            if (e.StartTime != e.EndTime)
            {
                e.duration = e.EndTime.Subtract(e.StartTime);
            }

            if (TimeChanged != null)
            {
                TimeChanged(this, e);
            }


        }
    }


//see timeevent args description below:

public class TimeEventArgs : EventArgs
{
    public TimeSpan duration;
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
    public bool keepCounting = false;

    public TimeEventArgs()
    {
        StartTime = DateTime.Now;
        EndTime = DateTime.Now;
        keepCounting = true;
    }
}


//See form class below:

  public partial class TimeApp : Form
{


    public TimeApp()
    {
        InitializeComponent();

    }

    private void startStopButton_Click(object sender, EventArgs e)
    {
        var theClock = new Clock();
        var timeApp = new TimeApp();
        timeApp.Subscribe(theClock);
        theClock.RunClock();

    }

    public void Subscribe(Clock theClock)
    {
        theClock.TimeChanged += new Clock.TimeChangedHandler(NewTime);
    }

    public void NewTime(object theClock, TimeEventArgs e)
    {

        displayBox.Text = e.duration.Hours.ToString() + ":"
            + e.duration.Minutes.ToString() + ":" + e.duration.Seconds.ToString();
    }


}

Upvotes: 1

Views: 80

Answers (2)

Cam Bruce
Cam Bruce

Reputation: 5689

You are suspending your main (UI) thread when calling Thread.Sleep(1000) - which is why your app is non-responsive.

Use a Timer (instead of Thread.Sleep()) and spin off any processing/long running code to a BackgroundWorker with for any processing you need to do. That way, your UI will stay responsive.

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564413

Your RunClock method blocks the UI (because of the Thread.Sleep(1000); call), which makes it impossible to stop.

Instead of looping, you should look at adding a Windows.Forms.Timer to your form, and using it to drive the clock.

Upvotes: 1

Related Questions