❔ Failing in breaking for loop

I cannot break out of for loop, somehow
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 break doesn't work here?
18 Replies
phaseshift
phaseshift10mo ago
Break works That's just not up for debate
With Blue Horns
With Blue Horns10mo ago
That's interesting since it does not work, let me open obs rq
phaseshift
phaseshift10mo ago
Why are you calling MoveForm when you're trying to stop it?!
With Blue Horns
With Blue Horns10mo ago
Because I can't have it 2 running in the same time, so I need to end first, then start second
phaseshift
phaseshift10mo ago
Ok, so these must be running on different threads then ...
With Blue Horns
With Blue Horns10mo ago
They cannot Because they will try to change position of form in the same time, which will result in jittering
With Blue Horns
With Blue Horns10mo ago
With Blue Horns
With Blue Horns10mo ago
Here, it stops for a second, then just continues going, which means, the loop wasn't broken But instead it should stop for a second, and go to bottom right corner
phaseshift
phaseshift10mo ago
The one way that setting stop moving=true can affect the loop from another method before it runs the next line is if the other Move Form is started from another thread
With Blue Horns
With Blue Horns10mo ago
My guess, it is started from another thread, because one is a thread that I created, second, is created by vs I guess? Because it's a looking for a click So I have 2 threads cool But shouldn't, in this case, give it a jittering like result? Like if it would go 2 ways at once, instead of just continuing?
phaseshift
phaseshift10mo ago
Probably one location = temp Point just gets ignored as they're both dispatched to the UI thread
With Blue Horns
With Blue Horns10mo ago
That's possible
phaseshift
phaseshift10mo ago
My advice is remove the loop with the sleep, use a timer event instead You can start/stop the timer
With Blue Horns
With Blue Horns10mo ago
I mean, that could work, but I'm unsure if it's a good approach, since I'm just trying to apply values of calculations I did before in time period But will definitly try it, I'm desperate for something that will work
phaseshift
phaseshift10mo ago
Using sleep is definitely a bad approach
With Blue Horns
With Blue Horns10mo ago
Yeah
phaseshift
phaseshift10mo ago
That is what the timer is for
Accord
Accord10mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.