Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C# Egg Timer Please Help!!!!

  • 09-11-2010 10:49PM
    #1
    Closed Accounts Posts: 3


    Hi Folks i am pretty new to c# and i cant get this to work. Basically its a timer application and when i start the thread i then cant use the stop or start button can anyone help. Here is my onpaint function where the threading happens


    I have set true initially to false


    protected override void OnPaint(PaintEventArgs pe)
    {
    base.OnPaint(pe);
    Graphics g = pe.Graphics;



    if (second >= 10)
    {
    g.DrawString(min + ":" + sec, f, Bbrush, 630, 30);

    }


    else if (second < 10)
    {

    g.DrawString(min + ":0" + sec, f, Bbrush, 630, 30);

    }
    if (start == true)
    {


    decrement_sec();
    Thread.Sleep(1000);
    sec = second.ToString();
    min = minute.ToString();
    this.Refresh();

    }


    Then this is what i have in my stop and start events


    Stop Event


    private void button2_Click(object sender, EventArgs e)
    {


    start = false;
    Console.WriteLine("Clicked");
    Application.DoEvents();
    Button b = sender as Button;
    this.Refresh();
    }


    Start Event

    private void button1_Click(object sender, EventArgs e)
    {
    start = true;
    this.Refresh();
    }



    and my decrement sec method is just this that im calling in the onPaint Function

    public void decrement_sec()
    {
    if (second > 0) second--;
    else
    {
    second = 59;
    if (minute > 0) minute--;
    }
    }



    Many thanks for the help


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    I didn't really look too closely to what you doing there but please note if you want to access any GUI code from outside of the main thread you will have to queue such events with the dispatcher.

    Have a look at Dispatcher.Invoke...

    And by the way, the Software Dev forum would be a better place.


  • Closed Accounts Posts: 3 clairey4u


    hmmm not sure how i use this in my program because my thread is a UI thread i dont know if that makes a difference.


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Moved to the Development forum.


  • Registered Users, Registered Users 2 Posts: 2,157 ✭✭✭dazberry


    clairey4u wrote: »
    protected override void OnPaint(PaintEventArgs pe)
            {
                base.OnPaint(pe);
                Graphics g = pe.Graphics;
    
                if (second >= 10)
                {
                    g.DrawString(min + ":" + sec, f, Bbrush, 630, 30);
                }
                else if (second < 10)
                {
                  g.DrawString(min + ":0" + sec, f, Bbrush, 630, 30);               
                }
              if (start == true)
               {             
                 decrement_sec();
                 [B]Thread.Sleep(1000);[/B]
                 sec = second.ToString();
                 min = minute.ToString();
                 [B]this.Refresh();[/B] 
               }
    

    The OnPaint handler isn't a separate thread, it's part of the main application thread, so Sleep(1000) will freeze your application for 1 second, and constantly forcing a refresh will keep it painting/freezing.

    Consider using a timer instead.

    D.


Advertisement