What is the best way to execute sequential methods?

Work on a project where a consistent set of methods should be performed every x seconds. Right now I have methods contained in another โ€œparent methodโ€, and just call them sequentially right after another.

 class DoTheseThings() { DoThis(); NowDoThat(); NowDoThis(); MoreWork(); AndImSpent(); } 

Each method should succeed without exception until the next step. So now I wrapped each of these methods with while and try..catch , and then in catch again executed this method.

 while( !hadError ) { try { DoThis(); } catch(Exception doThisException ) { hadError = true; } } 

It seems smelly and not very dry. Is there a better way to do this, so I am not wrapping new functions in the same methods. Isn't any collection of delegates properly implementing this?

Is there a more โ€œrightโ€ solution?

+4
source share
8 answers
 Action[] work=new Action[]{new Action(DoThis), new Action(NowDoThat), new Action(NowDoThis), new Action(MoreWork), new Action(AndImSpent)}; int current =0; while(current!=work.Length) { try { work[current](); current++; } catch(Exception ex) { // log the error or whatever // maybe sleep a while to not kill the processors if a successful execution depends on time elapsed } } 
+5
source

Isn't any collection of delegates properly implementing this?

Delegation is a possible way to solve this problem.

Just create a delegate something like this:

public delegate void WorkDelegate ();

and put them in an arraylist that you can iterate over.

+2
source

I have a personal religious belief that you should not catch System.Exception or, more precisely, you should catch exceptions that you know how to handle.

Speaking, I'm going to suggest that each of the methods you call does something different and may lead to different exceptions. This means that for each method, you probably need different handlers.

If you follow my religion, and the second statement is true, then you do not repeat the code unnecessarily. Unless you have other requirements, my recommendations for improving the code will be as follows:

1) Put a try-catch in each method, not around every method call.

2) Catchers in each method will ONLY catch the exceptions that you know about.

http://blogs.msdn.com/fxcop/archive/2006/06/14/631923.aspx http://blogs.msdn.com/oldnewthing/archive/2005/01/14/352949.aspx http: // www.joelonsoftware.com/articles/Wrong.html

Hth ...

+2
source

your example looks fine .. its dry but it will work well! in fact, if these methods access db .. you can use a transaction to ensure integrity ...

if you are dealing with shared variables for multiprocessor programs. Use synchronization cleanly. The most important thing in coding is that you write the correct code ... which has fewer errors .. and will complete the task correctly ..

+1
source
 public void DoTheseThings() { SafelyDoEach( new Action[]{ DoThis, NowDoThat, NowDoThis, MoreWork, AndImSpent }) } public void SafelyDoEach( params Action[] actions ) { try { foreach( var a in actions ) a(); } catch( Exception doThisException ) { // blindly swallowing every exception like this is a terrible idea // you should really only be swallowing a specific MyAbortedException type return; } } 
+1
source

What is the cause of the error?

If this is a resource problem, such as accessing something like a connection or object, then you may need to use monitors, semaphores, or just locking.

 lock (resource) { Dosomething(resource); } 

Thus, if the previous method accesses the resource, you can wait until it releases the resource to continue.

Ideally, you don't need to run a loop to execute something every time it fails. It does not work at all, you would like to know about the problem and fix it. Having a cycle to always keep trying is not the right way.

0
source

I would do what Ovidiu Pacurar offers, only I would use the foreach and leave the array indices to the compiler.

0
source

Delegate simple approach:

 Action<Action> tryForever = (action) => { bool success; do { try { action(); success = true; } catch (Exception) { // should probably log or something here... } } while (!success); }; void DoEverything() { tryForever(DoThis); tryForever(NowDoThat); tryForever(NowDoThis); tryForever(MoreWork); tryForever(AndImSpent); } 

Stack Method:

 void DoEverything() { Stack<Action> thingsToDo = new Stack<Action>( new Action[] { DoThis, NowDoThat, NowDoThis, MoreWork, AndImSpent } ); Action action; while ((action = thingsToDo.Pop()) != null) { bool success; do { try { action(); success = true; } catch (Exception) { } } while (!success); } 
0
source

All Articles