-3

On this code I am starting and stopping random sampling based on a fixed sampling time. When I clicked stop sampling, timers stops correctly. But when I start back again, the times do not start correctly. could you please help to check what is wrong here?

 public partial class MainWindow : Window
    {
       
        public float samplingTime = 10f;
        double currentDateTime = 0.0;
        DateTime dtEnd;
        public bool running;

        public MainWindow()
        {
            InitializeComponent();

            
            timer1 start.code()

            void timer_Tick(object sender, EventArgs e)
            {
              dtEnd = DateTime.Now.AddSeconds(samplingTime);   
               someCode();
            }
        }

        private void startSample_Click(object sender, RoutedEventArgs e)
        {
           
           timer2 start code()
            void windowtimer_Tick(object sender, EventArgs e)
            {
                currentDateTime = (dtEnd - DateTime.Now).TotalSeconds;
               
                if (currentDateTime < 0.1 && running == true)
                {
                    code();
                }

            }
        }

        private void stopSample_Click(object sender, RoutedEventArgs e)
        {
       
            timer.Stop();
            windowTimer.Stop();
            
        }
damu_d
  • 53
  • 1
  • 8
  • Could you post more of your codes. It looks like whenever you Start, you are adding a global variable. I guess that is the reson. – Nantharupan Jan 24 '21 at 13:45
  • I have updated. Thanks for help here! – damu_d Jan 24 '21 at 16:38
  • _"the times do not start correctly"_ is not a useful problem statement. Please explain _exactly_ what the code does, and _exactly_ how that's different from what you want, and _exactly_ what you've tried so far to fix it, and _exactly_ what _specifically_ you need help with. Make sure you provide an [mcve] that illustrates all of your explanations. – Peter Duniho Jan 24 '21 at 19:29

1 Answers1

1

You are adding the Tick handler multiple times:

windowTimer.Tick += windowtimer_Tick;

is executed every time you start, but you don't unhook it. Probably you should set the handler elsewhere (in the designer maybe).

Or you can add:

windowTimer.Tick -= windowtimer_Tick;

to the Stop handle

Charlieface
  • 52,284
  • 6
  • 19
  • 43
  • While you're correct that that issue is a serious problem, the OP has edited their question to remove that part of the problematic code (invalidating your answer), but more importantly, there's nothing in your answer that explains how fixing this will also fix the problem described as _"the times do not start correctly"_. Multiple subscriptions result in multiple invocations of the handler, not problems _starting_ a timer. – Peter Duniho Jan 24 '21 at 19:31
  • @PeterDuniho Given the original code and the rather unclear complaint *"the times do not start correctly"* I took an educated guess that it meant it was firing multiple times. I can't be responsible for further edits (it's not like I get alerted post facto to that) – Charlieface Jan 24 '21 at 20:02
  • _"Given the original code and the rather unclear complaint"_ -- the quality of the Stack Overflow site would be a lot higher if people would simply refrain from answering questions they don't actually fully understand. I doubt you've addressed the actual question, and posting an answer when the question is poorly presented just encourages users who can't be bothered to post a good question. – Peter Duniho Jan 24 '21 at 20:05