Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NT7 vs NT8 BarTimer: Choosing DispatcherTimer vs System.Timers.Timer?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    NT7 vs NT8 BarTimer: Choosing DispatcherTimer vs System.Timers.Timer?

    In reference to this thread,
    Hi. Using either hard-coded time values or a SessionIterator in attempt to place an order and/or do some operation in code at a certain time, to begin with OnBarUpdate has to be called, which it doesn't when there is no market action during that bar. This is the case in Historical state. 1) Preliminary Q: is the behavior in


    Originally posted by NinjaTrader_Jim View Post
    Hello digibob and bltdavid,

    You would be welcome to use any C# timer you would like but we should also be mindful of how that timer functions when incorporating it with NinjaScript.

    System.Timers.Timer can be used freely in the script when System.Windows.Threading.DispatcherTimer will be tied to the UI thread and would work if started from State.Configure or when invoked from ChartControl.Dispatcher (as in the BarTimer script.) I would recommend System.Timers.Timer for simplicity.
    Thanks, good to know!

    Btw, why does NT8 BarTimer use a Threading.DispatcherTimer? What does it mean for a
    timer to "be tied to the UI thread" and why does BarTimer need/want that?

    I ask this because,

    A) The NT7 BarTimer used System.Timers.Timer, but the NT8 BarTimer was changed
    to use DispatcherTimer, what technical reason prompted that change?

    B) In light of your recommendation, it seems useful to understand why NT8 BarTimer
    did not retain the simplicity of System.Timers.Timer, as the NT7 version did, esp since
    the NT7 code portions with System.Timers.Timer was already written and working.

    C) I am the author of bltEasyTimer, which combines the 4 indicators BarTimer,
    TickCounter, RangeCounter, and VolumeCounter into one universal timer/counter.
    Currently, bltEasyTimer is an NT7-only indicator, I would like to port this indicator
    to NT8, so would like to better understand the nuance of BarTimer's choice of
    timer object.

    #2
    Hello bltdavid,

    Thanks for your questions.

    The NT7 BarTimer script actually uses System.Windows.Forms.Timer. The choice to use System.Windows.Threading.DispatcherTimer in the NT8 version would just be to use a WPF timer as opposed to a WinForms timer. I could not comment for the preference of using System.Windows.Threading.DispatcherTimer vs. System.Timers.Timer in the BarTimer script; I would attribute this simply as a decision made to get the script converted.

    I would say the biggest lesson to note here is that since System.Windows.Threading.DispatcherTimer runs on the UI thread, we would have to start the timer there. This would involve either starting from State.Configure or by invoking through the UI Dispatcher. In the example attached, we can see that a System.Windows.Threading.DispatcherTimer cannot be started from State.DataLoaded, but can be from State.Configure or through a ChartControl.Dispatcher.

    Microsoft mentions that using System.Windows.Threading.DispatcherTimer could be preferred if you want the timer to run on the same thread as the UI and use DispatcherPriority with the timer. As mentioned in my quoted post, I would recommend using System.Timers.Timer for simplicity if this is not needed.

    If a System.Timers.Timer is used in a WPF application, it is worth noting that the System.Timers.Timer runs on a different thread than the user interface (UI) thread. In order to access objects on the user interface (UI) thread, it is necessary to post the operation onto the Dispatcher of the user interface (UI) thread using Invoke or BeginInvoke. Reasons for using a DispatcherTimer as opposed to a System.Timers.Timer are that the DispatcherTimer runs on the same thread as the Dispatcher and a DispatcherPriority can be set on the DispatcherTimer.
    Further reading (publicly available resource) - https://docs.microsoft.com/en-us/dot...tframework-4.8

    We look forward to being of further assistance.
    Attached Files

    Comment


      #3
      Good points, thanks for your reply!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      50 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      126 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      69 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      42 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      46 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X