Timer Control query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    Timer Control query

    Hi All

    I am using VB.net 2008 and use timer controls within my applications

    Question

    Does the code in a Timer control.tick event run on a different thread to the
    main Application thread (UI Thread)?

    In some of the timers I update some UI controls e.g statusbar.label s and I
    am wondering if I should be doing this if the code is running in a different
    thread


    Regards
    Steve


  • Jialiang Ge [MSFT]

    #2
    RE: Timer Control query

    Good morning Steve,

    Before we look at the two questions in the post, I'd like to first
    introduce three different timer classes in the .NET Class Library:

    System.Windows. Forms.Timer
    System.Timers.T imer
    System.Threadin g.Timer

    The first two classes appear in the Visual Studio.NET toolbox window,
    allowing us to drag and drop them directly onto a Windows Forms designer or
    a component class designer. System.Threadin g.Timer does not appear in the
    toolbox, but it exposes several more advanced features. Alex Calvo [MSFT]
    wrote a good article, going through the differences between the three in
    detail.

    Comparing the Timer Classes in the .NET Framework Class Library

    >Does the code in a Timer control.tick event run on a different thread
    to the main Application thread (UI Thread)?
    If the timer is the control dragged from the toolbox into the winform
    designer, control.tick event handler would run on the same thread as the UI
    thread. It can be proved with this piece of code:

    // in the winform, (e.g. Form_Load), we dump the UI thread ID:
    Debug.Print("UI Thread: " +
    Thread.CurrentT hread.ManagedTh readId.ToString ());

    // in the timer's tick event handler, we dump the ID of the thread that
    runs the handler:
    private void timer1_Tick(obj ect sender, EventArgs e)
    {
    Debug.Print("Ti ck: " +
    Thread.CurrentT hread.ManagedTh readId.ToString ());
    }
    >In some of the timers I update some UI controls e.g statusbar.label s
    and I am wondering if I should be doing this if the code is running in
    a different thread
    If the timer belongs to System.Windows. Forms.Timer, or System.Timers.T imer
    whose SynchronizingOb ject is set to the winform object (for example:

    Dim tmrTimersTimer As New System.Timers.T imer();
    tmrTimersTimer. SynchronizingOb ject = Me 'Synchronize with the current form

    the timer's tick event will run in the UI thread, and we can directly
    operate on the UI control (e.g. statusbar.label s).

    However, if the timer belongs to System.Threadin g.Timer, or
    System.Timers.T imer whose SynchronizingOb ject is NOT set to the winform
    object, the ticket event will run in a different thread, and we need to
    call either Control.Invoke or Control.BeginIn voke to operate on the UI
    controls. For example:

    // Created on UI thread
    private Label lblStatus;

    // Doesn't run on UI thread
    private void RunsOnWorkerThr ead() {
    DoSomethingSlow ();
    // Do UI update on UI thread
    object[] pList = { this, System.EventArg s.Empty };
    lblStatus.Begin Invoke(
    new System.EventHan dler(UpdateUI), pList);
    }

    // Code to be run back on the UI thread
    // (using System.EventHan dler signature
    // so we don't need to define a new
    // delegate type here)
    private void UpdateUI(object o, System.EventArg s e) {
    // Now OK - this method will be called via
    // Control.Invoke, so we are allowed to do
    // things to the UI.
    lblStatus.Text = "Finished!" ;
    }

    For simplicity, we usually write a wrapper function, e.g

    public void ShowProgress(st ring msg, int percentDone) {
    if (InvokeRequired ) {
    // As before
    } else {
    // We're already on the UI thread just
    // call straight through.
    UpdateUI(this, new MyProgressEvent s(msg,
    PercentDone));
    }
    }

    For more details, please refer to the MSDN magazine article:

    Give Your .NET-based Application a Fast and Responsive UI with Multiple
    Threads


    Is the above information helpful to you? If you have any other questions or
    concerns, please feel free to let me know.

    Have a very nice day!

    Regards,
    Jialiang Ge (jialge@online. microsoft.com, remove 'online.')
    Microsoft Online Community Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    msdnmg@microsof t.com.

    =============== =============== =============== =====
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subs...#notifications.

    MSDN Managed Newsgroup support offering is for non-urgent issues where an
    initial response from the community or a Microsoft Support Engineer within
    2 business day is acceptable. Please note that each follow up response may
    take approximately 2 business days as the support professional working with
    you may need further investigation to reach the most efficient resolution.
    The offering is not appropriate for situations that require urgent,
    real-time or phone-based interactions. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at

    =============== =============== =============== =====
    This posting is provided "AS IS" with no warranties, and confers no rights.



    Comment

    • Steve

      #3
      Re: Timer Control query

      Jialiang

      Thanks for a informative reply

      I should have given more information re the type of Timer I use

      I was referring to the System.Windows. Forms.Timer dragged from the toolbox

      So you have answered my query nicely

      Regards
      Steve

      ""Jialiang Ge [MSFT]"" <jialge@online. microsoft.comwr ote in message
      news:xNP4v2xOJH A.744@TK2MSFTNG HUB02.phx.gbl.. .
      Good morning Steve,
      >
      Before we look at the two questions in the post, I'd like to first
      introduce three different timer classes in the .NET Class Library:
      >
      System.Windows. Forms.Timer
      System.Timers.T imer
      System.Threadin g.Timer
      >
      The first two classes appear in the Visual Studio.NET toolbox window,
      allowing us to drag and drop them directly onto a Windows Forms designer
      or
      a component class designer. System.Threadin g.Timer does not appear in the
      toolbox, but it exposes several more advanced features. Alex Calvo [MSFT]
      wrote a good article, going through the differences between the three in
      detail.
      >
      Comparing the Timer Classes in the .NET Framework Class Library

      >
      >>Does the code in a Timer control.tick event run on a different thread
      >to the main Application thread (UI Thread)?
      >
      If the timer is the control dragged from the toolbox into the winform
      designer, control.tick event handler would run on the same thread as the
      UI
      thread. It can be proved with this piece of code:
      >
      // in the winform, (e.g. Form_Load), we dump the UI thread ID:
      Debug.Print("UI Thread: " +
      Thread.CurrentT hread.ManagedTh readId.ToString ());
      >
      // in the timer's tick event handler, we dump the ID of the thread that
      runs the handler:
      private void timer1_Tick(obj ect sender, EventArgs e)
      {
      Debug.Print("Ti ck: " +
      Thread.CurrentT hread.ManagedTh readId.ToString ());
      }
      >
      >>In some of the timers I update some UI controls e.g statusbar.label s
      >and I am wondering if I should be doing this if the code is running in
      >a different thread
      >
      If the timer belongs to System.Windows. Forms.Timer, or System.Timers.T imer
      whose SynchronizingOb ject is set to the winform object (for example:
      >
      Dim tmrTimersTimer As New System.Timers.T imer();
      tmrTimersTimer. SynchronizingOb ject = Me 'Synchronize with the current form
      >
      the timer's tick event will run in the UI thread, and we can directly
      operate on the UI control (e.g. statusbar.label s).
      >
      However, if the timer belongs to System.Threadin g.Timer, or
      System.Timers.T imer whose SynchronizingOb ject is NOT set to the winform
      object, the ticket event will run in a different thread, and we need to
      call either Control.Invoke or Control.BeginIn voke to operate on the UI
      controls. For example:
      >
      // Created on UI thread
      private Label lblStatus;
      >
      // Doesn't run on UI thread
      private void RunsOnWorkerThr ead() {
      DoSomethingSlow ();
      // Do UI update on UI thread
      object[] pList = { this, System.EventArg s.Empty };
      lblStatus.Begin Invoke(
      new System.EventHan dler(UpdateUI), pList);
      }
      >
      // Code to be run back on the UI thread
      // (using System.EventHan dler signature
      // so we don't need to define a new
      // delegate type here)
      private void UpdateUI(object o, System.EventArg s e) {
      // Now OK - this method will be called via
      // Control.Invoke, so we are allowed to do
      // things to the UI.
      lblStatus.Text = "Finished!" ;
      }
      >
      For simplicity, we usually write a wrapper function, e.g
      >
      public void ShowProgress(st ring msg, int percentDone) {
      if (InvokeRequired ) {
      // As before
      } else {
      // We're already on the UI thread just
      // call straight through.
      UpdateUI(this, new MyProgressEvent s(msg,
      PercentDone));
      }
      }
      >
      For more details, please refer to the MSDN magazine article:
      >
      Give Your .NET-based Application a Fast and Responsive UI with Multiple
      Threads

      >
      Is the above information helpful to you? If you have any other questions
      or
      concerns, please feel free to let me know.
      >
      Have a very nice day!
      >
      Regards,
      Jialiang Ge (jialge@online. microsoft.com, remove 'online.')
      Microsoft Online Community Support
      >
      Delighting our customers is our #1 priority. We welcome your comments and
      suggestions about how we can improve the support we provide to you. Please
      feel free to let my manager know what you think of the level of service
      provided. You can send feedback directly to my manager at:
      msdnmg@microsof t.com.
      >
      =============== =============== =============== =====
      Get notification to my posts through email? Please refer to
      http://msdn.microsoft.com/en-us/subs...#notifications.
      >
      MSDN Managed Newsgroup support offering is for non-urgent issues where an
      initial response from the community or a Microsoft Support Engineer within
      2 business day is acceptable. Please note that each follow up response may
      take approximately 2 business days as the support professional working
      with
      you may need further investigation to reach the most efficient resolution.
      The offering is not appropriate for situations that require urgent,
      real-time or phone-based interactions. Issues of this nature are best
      handled working with a dedicated Microsoft Support Engineer by contacting
      Microsoft Customer Support Services (CSS) at

      =============== =============== =============== =====
      This posting is provided "AS IS" with no warranties, and confers no
      rights.
      >
      >
      >

      Comment

      • Jialiang Ge [MSFT]

        #4
        Re: Timer Control query

        You are welcome, Steve.
        Glad to help!

        Regards,
        Jialiang Ge (jialge@online. microsoft.com, remove 'online.')
        Microsoft Online Community Support

        =============== =============== =============== ====
        Delighting our customers is our #1 priority. We welcome your comments and
        suggestions about how we can improve the support we provide to you. Please
        feel free to let my manager know what you think of the level of service
        provided. You can send feedback directly to my manager at:
        msdnmg@microsof t.com.

        This posting is provided "AS IS" with no warranties, and confers no rights.
        =============== =============== =============== ====

        Comment

        Working...