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  
               }
          }));

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;
                    }
                }
            });
4

2 回答 2

2

不要吞下异常,只在没有故障时运行 continue

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);
于 2013-08-14T15:09:34.803 回答
2

您需要使用ContinueWith 重载并允许TaskContinuationOptions执行冒泡

 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);
于 2013-08-14T15:10:05.853 回答