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 Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          571 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          331 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          549 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          549 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X