0

So, I have this class for dealing with timers in MVVM:

class RecognitionProcessTimer
    {
        DispatcherTimer analysisProcessTimer;
        public int Duration { get; set; }
        public RecognitionProcessTimer()
        {
            this.Duration = 500;
        }

        public void StartTimer()
        {
            analysisProcessTimer = new DispatcherTimer();
            analysisProcessTimer.Interval = TimeSpan.FromMilliseconds(this.Duration);
            analysisProcessTimer.Tick += AnalysisProcessTimer_Tick;
            analysisProcessTimer.Start();
        }

        public void StopTimer()
        {
            if (analysisProcessTimer != null)
            {
                analysisProcessTimer.Stop();
                analysisProcessTimer = null;
            }
        }

        private void AnalysisProcessTimer_Tick(object sender, object e)
        {
            analysisProcessTimer.Stop();
            // C# code current object variables from the other class
        }
    }

So, I have an analysis class that will do analysis on every tick of the timer. The RecognitionProcessTimer will be initialized and Start() on this analysis class's code but the timer's AnalysisProcessTimer_Tick event needs to check and call some current analysis class object variables. How do I go on achieving this?

I've tried to initialize the analysis class on tick method but then I lose all the data on there as a new object is used instead of the using the object that initialize the RecognitionProcessTimer class.

TLDR: I'm trying to use the variables from the class that initialized RecognitionProcessTimer from RecognitionProcessTimer's tick method.

If the question seems unclear, then please let me know and I'll clarify further. P.S: Can't use static variables.

dydx
  • 157
  • 15
  • How does the analysis class looks like? And why do you need to stop a timer after first tick? Actually, you can init everything you need in `RecognitionProcessTimer` constructor, or pass the parameters to it from class creating an instance of `RecognitionProcessTimer` class – Pavel Anikhouski Dec 28 '19 at 18:02
  • Is it a good practice to pass my class fields to the ``RecognitionProcessTimer`` as a parameter? The analysis class has a few non-static variables that I need to check in ``Tick`` before calling the analysis method from ``Tick``. Also, how would I go about calling my analysis class methods from the ``AnalysisProcessTimer_Tick`` method? Remember, I need to reuse the object that initialized the ``RecognitionProcessTimer``. – dydx Dec 28 '19 at 18:06
  • It's a good practice to pass all class dependencies to constructor, or through properties. Anyway, it's hard to say something without context and details – Pavel Anikhouski Dec 28 '19 at 18:09
  • I think I've provided enough context. Let me know what exactly you're looking for. – dydx Dec 28 '19 at 18:10

0 Answers0