Joh*_*ell 4 c# delegates exception
处理必须每秒运行一系列顺序方法的项目x.现在我将方法包含在另一个"父方法"中,然后依次顺序调用它们.
class DoTheseThings()
{
DoThis();
NowDoThat();
NowDoThis();
MoreWork();
AndImSpent();
}
Run Code Online (Sandbox Code Playgroud)
每个方法必须成功运行,而不会在下一步完成之前抛出异常.所以现在我用a while和那些方法包装每个方法try..catch,然后catch再次执行那个方法.
while( !hadError )
{
try
{
DoThis();
}
catch(Exception doThisException )
{
hadError = true;
}
}
Run Code Online (Sandbox Code Playgroud)
这看起来很臭,而且不是很干.有没有更好的方法来做到这一点,所以我没有在相同的方法中包装任何新的功能.是不是某种Delegate集合实现这个的正确方法?
有更"适当"的解决方案吗?
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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2534 次 |
| 最近记录: |