Repeating a function in C # until it no longer throws an exception

I have a class that calls the SOAP interface and returns an array of data. However, if this request expires, it throws an exception. It's good. However, I want my program to try again to make this call. If it expires, I would like it to continue to make this call until it succeeds. How can i do this?

For instance:

try { salesOrdersArray = MagServ.salesOrderList(sessID, filter); } catch { ?? What Goes Here to FORCE the above line of code to rerun until it succeeds. } 
+6
source share
11 answers

You just need to go in cycles forever:

 while (true) { try { salesOrdersArray = MagServ.salesOrderList(sessID, filter); break; // Exit the loop. Could return from the method, depending // on what it does... } catch { // Log, I suspect... } } 

Please note that you will almost certainly not actually quote forever. You should almost certainly have the maximum number of attempts, and probably only catch certain exceptions. Catching all exceptions forever can be horrible ... imagine if salesOrderList (the name of the non-traditional method, btw) throws an ArgumentNullException because you have an error and filter is null ... do you really want to bind 100% of your processor forever?

+14
source

If you can not change the timeout, below will work. salesOrdersArray should be initialized to null .

 while(salesOrdersArray == null) { try { salesOrdersArray = MagServ.salesOrderList(sessID, filter); } catch { // Log failure } } 
+4
source

It is not an ideal ideal to use exceptions as a control flow, but it will do what you requested.

 bool Caught = true; while (Caught) try { salesOrdersArray = MagServ.salesOrderList(sessID, filter); Caught = false; } catch { Caught = true; } 
+1
source

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.

+1
source

I will use a transactional queue (MSMQ) to store a service call. The loop will deactivate the messages and call the service in TransactionScope, if the call fails, the message will remain in the queue. The timeout can be specified by adding the expiration time in the message. This solution is good if you really want a reliable solution, as I guessed that calling this operation is crucial.

+1
source
 bool repeat = true; while (repeat) { try { salesOrdersArray = MagServ.salesOrderList(sessID, filter); repeat = false; } catch { } } 
0
source

Try

 bool failed = false; do { try { salesOrdersArray = MagServ.salesOrderList(sessID, filter); } catch { failed = true; } } while(failed); 

The behavior that you after this can cause an infinite loop if it never succeeds, though ...

0
source

Try something like this:

 var failed = true; while (failed) { try { salesOrdersArray = MagServ.salesOrderList(sessID, filter); failed = false; } catch { } } 

Edit: Wow! Great minds think alike! :)

0
source

Although I would not recommend that you do this infinitely many times, you can make a separate function from this sentence:

 void GoConnect() { try { salesOrdersArray = MagServ.salesOrderList(sessID, filter); } catch { GoConnect(); } } 
0
source
 while(salesOrdersArray == null){ try { salesOrdersArray = MagServ.salesOrderList(sessID, filter); } catch(salesOrderException e) { log(e.message); } } 

This will work forever and uses exceptions as a slow loop. Is there a way you can change your function to return null instead of throwing an exception? If you expect this call to be interrupted regularly, do not use the try / catch block.

0
source

I follow this diagram to solve this problem:

  public void Send(String data, Int32 attemptNumber) { try { yourCodeHere(data); } catch (WebException ex) { if (attemptNumber > 0) Send(data, --attemptNumber); else throw new AttemptNumberExceededException("Attempt number exceeded!", ex); } catch (Exception ex) { //Log pourpose code goes here! throw; } } 

Trying forever does not seem to be a good idea, as you can end up having an endless process. If you think that you need a lot of attempts to achieve your goal, just install a huge amount here.

I personally find it reasonable to wait a few milliseconds, or a second, after trying eac Thread.Sleep (1000); before sending callig (data); --- You could, for example, use the attempNumber variable to call or decrease this timeout if you consider it reasonable for your scenario.

0
source

Source: https://habr.com/ru/post/900229/


All Articles