为了这次检讨,我有以下缩短的方法。实际方法有更多的任务。本质上,我要做的是创建许多任务,启动它们,等待它们全部完成,并处理生成的任何异常。这个看起来对吗?
public static void RefreshCoaterCaches()
{
try
{
CicApplication.CoaterDataLock.EnterWriteLock();
List<Task> alarmTasks = new List<Task>();
alarmTasks.Add(Task.Factory.StartNew(() =>
{
using (DistributorBackpressure44Logic logic = new DistributorBackpressure44Logic())
logic.RefreshDistributorBackpressure44Cache();
}));
alarmTasks.Add(Task.Factory.StartNew(() =>
{
using (DeltaPressureLogic logic = new DeltaPressureLogic())
logic.RefreshDeltaPressureCache();
}));
Task.WaitAll(alarmTasks.ToArray());
}
catch (System.AggregateException exception)
{
CicServerLogic.HandleUnexpectedAggregateException(exception.Flatten(), string.Empty);
}
catch (System.Exception exception)
{
CicServerLogic.HandleUnexpectedException(exception);
}
finally
{
CicApplication.CoaterDataLock.ExitWriteLock();
}
}然后,我使用这个方法来处理抛出的任何AggregateException:
internal static void HandleUnexpectedAggregateException(AggregateException aggregateException)
{
try
{
foreach (Exception exception in aggregateException.InnerExceptions)
{
...
...
Do some stuff with the current exception, like logging etc.
...
}
}
catch (Exception)
{
}
}发布于 2012-11-27 11:30:48
我建议考虑如下:
这里稍微修改了代码的版本:
private static Task GetDistributorBackpressure44LogicAsync()
{
Task.Factory.StartNew( () =>
{
using (DistributorBackpressure44Logic logic = new DistributorBackpressure44Logic())
logic.RefreshDistributorBackpressure44Cache();
}
}
private static Task DeltaPressureLogicAsync()
{
Task.Factory.StartNew(() =>
{
using (DeltaPressureLogic logic = new DeltaPressureLogic())
logic.RefreshDeltaPressureCache();
}
}
public static void RefreshCoaterCaches()
{
try
{
CicApplication.CoaterDataLock.EnterWriteLock();
var t1 = GetDistributorBackpressure44LogicAsync();
var t2 = DeltaPressureLogicAsync();
// You can switch back to you list-based approach if you'll have
// much more other operations
Task.WaitAll(t1, t2);
}
catch (System.AggregateException exception)
{
// I'm really not sure that its a good idea to create "generic"
// exception handling scheme. Maybe you should handle them manually right here.
// And what is string.Empty is all about?
CicServerLogic.HandleUnexpectedAggregateException(exception.Flatten(), string.Empty);
}
// You should not catch System.Exception thats why I removed it
finally
{
CicApplication.CoaterDataLock.ExitWriteLock();
}
}现在,关于泛型异常处理程序(我已经提到过这种技术可能导致错误隐藏和复杂的“泛型”逻辑)(请参阅我在代码中的注释):
internal static void HandleUnexpectedAggregateException(AggregateException aggregateException)
{
// What are we afraiding of, why we're using try/catch here?
// We should now swallow unknown exception because it can lead to
// hiding bugs in our code!
try
{
// This technique is ok
foreach (Exception exception in aggregateException.InnerExceptions)
{
...
...
Do some stuff with the current exception, like logging etc.
...
}
}
catch (Exception)
{
// I strongly not recommended to use empty catch blocks!!
}
}https://codereview.stackexchange.com/questions/19028
复制相似问题