Was ist der beste Weg, um eine Ausnahme in Task abzufangen?

Mit System.Threading.Tasks.Task<TResult> habe ich zur Verwaltung der Ausnahmen, die geworfen werden konnte. Ich bin auf der Suche nach der beste Weg, das zu tun. So weit, ich habe eine base-Klasse, verwaltet alle die nicht abgefangene Ausnahmen innerhalb der call of .ContinueWith(...)

Frage ich mich, ob es einen besseren Weg, tun, tun. Oder sogar, wenn es ist ein guter Weg, das zu tun.

public class BaseClass
{
    protected void ExecuteIfTaskIsNotFaulted<T>(Task<T> e, Action action)
    {
        if (!e.IsFaulted) { action(); }
        else
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
            {
                /* I display a window explaining the error in the GUI 
                 * and I log the error.
                 */
                this.Handle.Error(e.Exception);
            }));            
        }
    }
}   

public class ChildClass : BaseClass
{
    public void DoItInAThread()
    {
        var context = TaskScheduler.FromCurrentSynchronizationContext();
        Task.Factory.StartNew<StateObject>(() => this.Action())
                    .ContinueWith(e => this.ContinuedAction(e), context);
    }

    private void ContinuedAction(Task<StateObject> e)
    {
        this.ExecuteIfTaskIsNotFaulted(e, () =>
        {
            /* The action to execute 
             * I do stuff with e.Result
             */

        });        
    }
}

InformationsquelleAutor der Frage JiBéDoublevé | 2012-10-19

Schreibe einen Kommentar