locked
Timer callbacks, Invoke() or .BeginInvoke(); RRS feed

  • Question

  • I have a form with a button and a label. I want to update the label on the form/UI thread with the value in the print time method. I know that you need to pass a delegate (I think this should be the timer call back) but I'm a little stuck. Here's what I have so far,

     

    Code Snippet

    using System;

    using System.Windows.Forms;

    namespace TimerBallBackTest

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    System.Threading.TimerCallback tcb = new System.Threading.TimerCallback(PrintTime);

    System.Threading.Timer t = new System.Threading.Timer(

    tcb,

    "Hello",

    1000,

    1000);

    }

    private void PrintTime(object state)

    {

    if (label1.InvokeRequired)

    {

    label1.Text = DateTime.Now.ToLongTimeString();

    }

    }

    }

    }

     

     

     

     

     

    Monday, May 19, 2008 11:38 AM

Answers

  • Hi vesuvius,

     

           If invoke required is true, that mean the function you are calling is on different thread and your control is created on different thread. Hence in this case you need to set text like this:

     

           

    Code Snippet

    using System;

    using System.Windows.Forms;

    namespace TimerBallBackTest

    {

    public partial class Form1 : Form

    {

    delegate void SetLableText();

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    System.Threading.TimerCallback tcb = new System.Threading.TimerCallback(PrintTime);

    System.Threading.Timer t = new System.Threading.Timer(

    tcb,

    "Hello",

    1000,

    1000);

    }

    private void PrintTime(object state)

    {

        if (label1.InvokeRequired)

        {

    label1.Invoke(new SetLableText(SetText));

        }

    }

    private void SetText()

    {

         label1.Text = DateTime.Now.ToLongTimeString();

    }

    }

    }

     

     

     

    Sajal

    Monday, May 19, 2008 12:32 PM

All replies

  • Hi vesuvius,

     

           If invoke required is true, that mean the function you are calling is on different thread and your control is created on different thread. Hence in this case you need to set text like this:

     

           

    Code Snippet

    using System;

    using System.Windows.Forms;

    namespace TimerBallBackTest

    {

    public partial class Form1 : Form

    {

    delegate void SetLableText();

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    System.Threading.TimerCallback tcb = new System.Threading.TimerCallback(PrintTime);

    System.Threading.Timer t = new System.Threading.Timer(

    tcb,

    "Hello",

    1000,

    1000);

    }

    private void PrintTime(object state)

    {

        if (label1.InvokeRequired)

        {

    label1.Invoke(new SetLableText(SetText));

        }

    }

    private void SetText()

    {

         label1.Text = DateTime.Now.ToLongTimeString();

    }

    }

    }

     

     

     

    Sajal

    Monday, May 19, 2008 12:32 PM
  • Thats it!

     

    Thanks a lot Sajal.

    Monday, May 19, 2008 12:59 PM