You should put a try / catch block inside the loop construct. If you do not want to consume 100% of your processor, put Thread.Sleep in the catch block, so every time an exception occurs, it will wait for a while, freeing the processor from other things.
// iterate 100 times... not forever! for (int i = 0; i < 100; i++) { try { // do your work here; break; // break the loop if everything is fine } catch { Thread.Sleep(1000); } }
You can also specify the type of exception, so that only the timeout exception and the passing of other types of exceptions are processed.
// iterate 100 times... not forever! for (int i = 0; i < 100; i++) { try { // do your work here; break; // break the loop if everything is fine } catch (TimeOutException) { Thread.Sleep(1000); } }
Note that a TimeOutException must be replaced with the real name of the exception ... I do not know if this is the real name.
Also adjust the sleep time given in milliseconds and the number of repetitions, in the event that I have presented, 100 repetitions of 1000 ms give a maximum waiting period of 1 minute and 40 seconds plus the operating time.
source share