Click here to Skip to main content
14,736,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
}
Posted
Updated 1-Jan-18 19:35pm

1 solution

You're not actually starting the _MotorMove Task. Use Task.Run instead of new Task; however, the Wait does not work as you're expecting it too. Wait will throw an exception if the Task does not complete within that time, but it will continue as soon as it completes.

I would use continuation tasks here and put a Task.Delay at the end for any delay needed. Alternatively you can use a ManualResetEvent or Monitor for more granular control.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900