Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Doesn't exist in current context.

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

    Doesn't exist in current context.

    I am sure this is dead easy if you know how. I am trying to do some timing using the stopwatch class (recommended over datetime).

    In initialize I have
    Stopwatch timer = new Stopwatch();
    timer.Start();

    How do I access timer in OnBarUpdate? I tried adding time to the #region variable but that caused run time errors. How should I do this properly?

    Many thanks.

    #2
    NickA, since is this more general C# this is unfortunately outside of the scope we can support here, but I believe you would need to track the stopWatch.Elapsed with a timeSpan object?

    Comment


      #3
      Appreciate the info. The problem I have is far more basic than that. Stated generally (so hopefully not beyond the scope of support )

      How/where do I instantiate an object O of Class C so I can initialise it in OnStartUp (or Initialize) and access it OnBarUpdate?

      I am getting run time errors "Object reference not set to an instance of an object." if I instantiate it in OnStartUp when I access it. I guess its kind of a 'scope' thing.

      Comment


        #4
        You're correct, this is related to the scope of the object definition - for example this simple test in OnBarUpdate() works ok for me -

        protected override void OnBarUpdate()
        {
        if (Historical) return;

        Stopwatch stopWatch = new Stopwatch();

        stopWatch.Start();

        TimeSpan ts = stopWatch.Elapsed;

        Print(ts.ToString());
        }

        Comment


          #5
          Thanks Bertrand appreciate it. I finally sussed it.

          I needed to have
          timer = new Stopwatch()

          rather than
          Stopwatch timer = new Stopwatch();

          The latter creates an instantiates a new timer with scope that only extends to NT's Initialize rather than instantiating my global timer object.

          C# is not my mother tongue.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Hwop38, 05-04-2026, 07:02 PM
          0 responses
          155 views
          0 likes
          Last Post Hwop38
          by Hwop38
           
          Started by CaptainJack, 04-24-2026, 11:07 PM
          0 responses
          307 views
          0 likes
          Last Post CaptainJack  
          Started by Mindset, 04-21-2026, 06:46 AM
          0 responses
          244 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by M4ndoo, 04-20-2026, 05:21 PM
          0 responses
          346 views
          0 likes
          Last Post M4ndoo
          by M4ndoo
           
          Started by M4ndoo, 04-19-2026, 05:54 PM
          0 responses
          176 views
          0 likes
          Last Post M4ndoo
          by M4ndoo
           
          Working...
          X