❔ Failing in breaking for loop
I cannot break out of for loop, somehow
So, since moment of calling Event1 is unknown, it might interfere with MovementManager, so, I need it to break the for loop if needed, and then proceed to call MoveForm that is in Event1. Any suggestions how to fix this? Any idea why
bool StopMoving = false;
public void Event1(int hp)
{
StopMoving = true;
MoveForm(0, 0, 1);
//This void is called when user clicked let's say a button
//Because of that, it can be called anytime
//Change the StopMoving to true to stop for loop, then call the function?
}
private void MovementManager()
{
MoveForm(600, 200, 4);
//This void is called upon start of the program
}
private void MoveForm(int targetX, int targetY, int time)
{
int framerate = 60;
int newTime = time * framerate;
int DistanceX = this.DesktopLocation.X - targetX;
int DistanceY = this.DesktopLocation.Y - targetY;
//Per Time Unit
int PTUX = DistanceX / (int)newTime;
int PTUY = DistanceY / (int)newTime;
Debug.Print($"Pos: {targetX}X {targetY}Y; Dis: {DistanceX}X {DistanceY}Y");
for (int i = newTime; i > 0; i--)
{
Thread.Sleep(1000 / framerate);
if (StopMoving)
{
StopMoving = false;
break; // This should exit the for loop, but doesn't?
}
Point tempPoint = new Point(this.DesktopLocation.X + PTUX, this.DesktopLocation.Y + PTUY);
this.Invoke((MethodInvoker)delegate
{
this.DesktopLocation = tempPoint;
});
}
}bool StopMoving = false;
public void Event1(int hp)
{
StopMoving = true;
MoveForm(0, 0, 1);
//This void is called when user clicked let's say a button
//Because of that, it can be called anytime
//Change the StopMoving to true to stop for loop, then call the function?
}
private void MovementManager()
{
MoveForm(600, 200, 4);
//This void is called upon start of the program
}
private void MoveForm(int targetX, int targetY, int time)
{
int framerate = 60;
int newTime = time * framerate;
int DistanceX = this.DesktopLocation.X - targetX;
int DistanceY = this.DesktopLocation.Y - targetY;
//Per Time Unit
int PTUX = DistanceX / (int)newTime;
int PTUY = DistanceY / (int)newTime;
Debug.Print($"Pos: {targetX}X {targetY}Y; Dis: {DistanceX}X {DistanceY}Y");
for (int i = newTime; i > 0; i--)
{
Thread.Sleep(1000 / framerate);
if (StopMoving)
{
StopMoving = false;
break; // This should exit the for loop, but doesn't?
}
Point tempPoint = new Point(this.DesktopLocation.X + PTUX, this.DesktopLocation.Y + PTUY);
this.Invoke((MethodInvoker)delegate
{
this.DesktopLocation = tempPoint;
});
}
}So, since moment of calling Event1 is unknown, it might interfere with MovementManager, so, I need it to break the for loop if needed, and then proceed to call MoveForm that is in Event1. Any suggestions how to fix this? Any idea why
breakbreak doesn't work here?