从父任务抛出异常时停止继续任务

时间:2013-08-14 15:05:20

标签: c# multithreading task-parallel-library

我想确保在并行循环中抛出一个延期时不会发生延续任务

 var parent = tf.StartNew(() =>

    Parallel.ForEach(QuestionsLangConstants.questionLangs.Values, (i, state) =>
          {
             try
               {
                 qrepo.UploadQuestions(QWorkBook.Worksheets[i.QSheet],
                 QWorkBook.Worksheets[i.QTranslationSheet], i, prog);
               }
             catch (Exception ex)
               {

                 context.Dispose();
                 state.Break();
                //make sure the execution fails  
               }
          }));

var finalTast = parent.ContinueWith(i =>
            {
                if (context != null)
                {
                    DialogResult result = 
     MessageBox.Show("Do You Want to Commit the Questions?", "Save to DB",
      MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (result.Equals(DialogResult.OK))
                    {
                       //Do Stuff here
                    }
                    else
                    {
                        return;
                    }
                }
            });

2 个答案:

答案 0 :(得分:2)

请勿吞下异常,并且仅在出现故障时继续运行

var parent = tf.StartNew(() =>

                        Parallel.ForEach(QuestionsLangConstants.questionLangs.Values, (i, state) =>
                        {
                            try
                            {
                                qrepo.UploadQuestions(QWorkBook.Worksheets[i.QSheet], QWorkBook.Worksheets[i.QTranslationSheet], i, prog);

                            }
                           catch (Exception ex)
                           {

                              context.Dispose();
                                state.Break();
                             //make sure the execution fails
                             throw;
                            }
                        }));

var finalTast = parent.ContinueWith(i =>
            {
                if (context != null)
                {
                    DialogResult result = MessageBox.Show("Do You Want to Commit the Questions?", "Save to DB", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (result.Equals(DialogResult.OK))
                    {
                       //Do Stuff here
                    }
                    else
                    {
                        return;
                    }
                }
        }, TaskContinuationOptions.NotOnFaulted);

答案 1 :(得分:2)

您需要使用TaskContinuationOptions中的ContinueWith overload并允许异常冒泡

 var parent = tf.StartNew(() =>

    Parallel.ForEach(QuestionsLangConstants.questionLangs.Values, (i, state) =>
          {
             try
               {
                 qrepo.UploadQuestions(QWorkBook.Worksheets[i.QSheet],
                 QWorkBook.Worksheets[i.QTranslationSheet], i, prog);
               }
             catch (Exception ex)
               {

                 context.Dispose();
                 state.Break();
                //make sure the execution fails  
                throw; //<-- This line was added to stop the continuation task.
               }
          }));

var finalTast = parent.ContinueWith(i =>
            {
               //...
            }, TaskContinuationOptions.OnlyOnRanToCompletion);