i tried several things but they did not succeed. Can somone help me please?
I want to use a for loop, the for loop has to trigger a dispatcher timer, BUT has to wait until the timer has finished to continue the loop. Thanks in advance!
What I have tried:
private async void LoopHead(object sender, EventArgs e)
{
HomeM.SystemInfo = "Bezig met loopen...";
int number = DOORLOOPTEST.Properties.Settings.Default.LOOPS * 2;
sbyte HeadUpTime = Convert.ToSByte(((DOORLOOPTEST.Properties.Settings.Default.TimeHead_UP).ToString()).Remove(0, 6));
sbyte HeadDownTime = Convert.ToSByte(((DOORLOOPTEST.Properties.Settings.Default.TimeHead_DOWN).ToString()).Remove(0, 6));
DOORLOOPTEST.Properties.Settings.Default.LOOPS_DONE = 0;
DOORLOOPTEST.Properties.Settings.Default.Save();
DOORLOOPTEST.Properties.Settings.Default.LOOPS_TODO = DOORLOOPTEST.Properties.Settings.Default.LOOPS;
DOORLOOPTEST.Properties.Settings.Default.Save();
for (int i = 0; i < DOORLOOPTEST.Properties.Settings.Default.LOOPS; i++)
{
string value = DOORLOOPTEST.Properties.Settings.Default.UP_or_DOWN;
switch (value)
{
case "UP":
_time = TimeSpan.FromSeconds(HeadUpTime);
HomeM.PIN_ON = DOORLOOPTEST.Properties.Settings.Default.PMHU_ON;
HomeM.PIN_OFF = DOORLOOPTEST.Properties.Settings.Default.PMHU_OFF;
_MotorMove = new Task(() => MotorMove(sender, e, _time));
_MotorMove.Wait((HeadUpTime + 3) * 1000);
DOORLOOPTEST.Properties.Settings.Default.UP_or_DOWN = "DOWN";
DOORLOOPTEST.Properties.Settings.Default.Save();
break;
case "DOWN":
_time = TimeSpan.FromSeconds(HeadDownTime);
HomeM.PIN_ON = DOORLOOPTEST.Properties.Settings.Default.PMHD_ON;
HomeM.PIN_OFF = DOORLOOPTEST.Properties.Settings.Default.PMHD_OFF;
_MotorMove = new Task(() => MotorMove(sender, e, _time));
_MotorMove.Wait((HeadUpTime + 3) * 1000);
DOORLOOPTEST.Properties.Settings.Default.UP_or_DOWN = "UP";
DOORLOOPTEST.Properties.Settings.Default.Save();
DOORLOOPTEST.Properties.Settings.Default.LOOPS_DONE += 1;
DOORLOOPTEST.Properties.Settings.Default.Save();
DOORLOOPTEST.Properties.Settings.Default.LOOPS_TODO = DOORLOOPTEST.Properties.Settings.Default.LOOPS -= 1;
DOORLOOPTEST.Properties.Settings.Default.Save();
HomeM.LoopsDone = DOORLOOPTEST.Properties.Settings.Default.LOOPS_DONE.ToString();
HomeM.LoopsToDo = DOORLOOPTEST.Properties.Settings.Default.LOOPS_TODO.ToString();
break;
}
}
}
private void MotorMove(object sender, EventArgs e, TimeSpan _time)
{
TimerBegint(sender, e);
LOOP_timer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
{
if (_time == TimeSpan.Zero)
{
LOOP_timer.Stop();
LOOP_timer.Tick += TimerIsGedaan;
}
_time = _time.Add(TimeSpan.FromSeconds(-1));
}, Application.Current.Dispatcher);
LOOP_timer.Start();
}