I have a while loop that waits for a local setting value to no longer be null. In the event that something is borked and the value remains null, is there some sort of best practice for how you should break out of the loop? Right now I just have it counting up to 100 and if it's looped 100 times to break the loop:
int i = 0;
while (ApplicationData.Current.LocalSettings.Values["MediaInfoSaved"] == null)
{
await Task.Delay(15);
i ;
if(i >= 100)
{
break;
}
}
The loop is waiting for a process in a full trust helper to finish and return it's result. The process can take a while to complete sometimes (0.1-1 second).
CodePudding user response:
Put the value (represented as 100 in your code) in a config file so it's easily changed if needed. If issues pop up later you can adjust it as needed.
CodePudding user response:
It is very common practice to implement a timeout, where you wait a specified amount of time (if you have an idea how long the process should be taking) before breaking out of the loop. This is basically what you have implemented. If you think it shouldn't take more than 1 second to complete, make sure it breaks the loop after 1.5-2 seconds. There may be more effective ways to implement this, like using the actual time within the system.
