Rewriting the code helps to explain why:
while (someCondition)
{
System.Threading.Thread.Sleep(500);
Application.Current.Dispatcher.Invoke(MyMethod);
}
private void MyMethod()
{
if (someOtherCondition)
{
// Do stuff...
}
else
{
if (anotherCondition)
{
break;
}
// Do something else...
}
}
You are breaking inside a function that has no loop. The loop exists in another method. So return needs to be called instead, as you found out. Just because you are using an annonymous method, it's still a separate method to the one containing the while loop.