There is a functional that works asynchronously in a separate thread. After 3 minutes have elapsed, it is necessary to interrupt the execution. Something like pseudocode like:

Timer1.start() //Запустить таймер funct_work(); // Запускаем функционал while (Timer1.value() < 3); // проверяем прошло ли 3 минуты, если нет продолжаем работу. Timer1.Stop(); // время работы вышло 

While at the moment it turned out this:

 funct_work(); System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); timer.Interval = 1000; timer.Tick += new EventHandler(timer_Tick); // в функции timer_tick идет увеличение глобальной переменной timer.Start(); if (glob_time < 180){;}else{timer.Stop()} 

However, the problem is that the condition is satisfied 1 time. If you put a loop, the timer_tick () function is not executed.

Helped to make an announcement System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer (); globally. Move out timer.Tick + = new EventHandler (timer_Tick); in main (for each time the function was started, another handler was hung and the timer was accelerated) Check glob_time was sent to the timer_Tick function. After that, everything worked.

  • AND? What did not work out? - Andrew NOP
  • Not my main language, because I can provide only pseudocode, I would like the correct syntax. + As far as I understand the ad in the main thread, it leads to its hangup (until the cycle ends). - KordDEM
  • What type of application: WinForms, WPF, more? Add a tag. - Alexander Petrov
  • Added a tag, painted a little what happened - KordDEM
  • Strange code or these pieces are incomprehensible. It even seems to be working as it should, it will not, because when it if (glob_time < 180){ condition if (glob_time < 180){ - it will not work again. Use the task scheduler. - And

0