tags:

views:

49

answers:

3

i'm getting an odd execption on task creation in .net 4.0. i'm getting the exectpion on windows service with a Global Updomain unhandled execptions handler so i don't have the exact stack-"A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property." i think it occurs on the following code section:

  for (int i = 0; i < _workers.Length; ++i)
        {
            int j = i;  // copy 

            Task t1 = Task.Factory.StartNew(() =>
            {
                try
                {
                    if (!_workers[j].Join(4000))
                        LogWriter.Trace("Failed to join thread", "ThreadFailureOnDispose");
                }
                catch (Exception ex)
                {
                    OnLogged(ex.Message + ex.StackTrace);
                }
            });

        }

anyone got an idea? is it something with the aggreated exception feature?

A: 
Richard
how should i do it? any examples?
@user437631 For debugging look at the output, identify what's failing and fix it. At runtime handle cases you can handle at runtime inside the task, or a Continuation set for exceptions. I.e. as for any exception.
Richard
A: 

well, I think that Task should catch AggregateExecption when using Parallel.For\Foreach as follows:

  try
        {
            Parallel.For(0, _workers.Length, i =>
            {

                DoWork(i);

            });

        }
        catch (AggregateException ex)
        {
            // Assume we know what's going on with this particular exception.
            // Rethrow anything else. AggregateException.Handle provides
            // another way to express this. See later example.
            foreach (var e in ex.InnerExceptions)
            {
                OnLogged(e.Message + e.StackTrace);
            }

        }
+1  A: 

See the bottom of

http://blogs.msdn.com/b/pfxteam/archive/2009/10/27/9913610.aspx

for some useful info.

I don't think this is coming from the code you have showed in the question.

Brian