Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C# Egg Timer Please Help!!!!

  • 09-11-2010 9: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,152 ✭✭✭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