1

I have private method, which try to generate pdf files and then send email with this pdfs. If all is OK then this method change text on form to "Pdf were generated successfully".

My method :

private void RegenerateSubmissionPdf(int submissionId)
{
    var submissionPath = PublisherConfigurationManager.SubmissionPath + submissionId;

    var tasks = new List<Task>
    {
        HttpContext.Current.GeneratePdfTask(submissionPath, submissionId, PublisherConst.SubmissionPdfName,
            _objSubmission.SaveSubmissionPdf)
    };

    tasks.SendEmailTasks(u => SendRegenerateSubmissionPdfEmail(submissionId, u));
}

And method that returns Task :

public static Task GeneratePdfTask(this HttpContext context, string path, int submissionId, string pdfName, Action<int, byte[]> postAction = null)
{
    var local = context;
    return Task.Factory.StartNew(() =>
    {
        HttpContext.Current = local;
        SessionHelper.Set(SessionKey.IsPdfRendering, true);
        var pdfFile = new PdfGenerator().Generate(path, pdfName, submissionId);
        if (postAction != null && pdfFile != null)
        {
            postAction(submissionId, pdfFile);
        }

    });
}

In last method sometimes I got exception (maybe path is wrong, some image is missing or something else). I found this solution, but don't know how to apply it to my case.

Community
  • 1
  • 1
demo
  • 6,038
  • 19
  • 75
  • 149

2 Answers2

0

Have you tried this:

private void RegenerateSubmissionPdf(int submissionId)
{
    var submissionPath = PublisherConfigurationManager.SubmissionPath + submissionId;

    var tasks = new List<Task>
    {
        HttpContext.Current.GeneratePdfTask(submissionPath, submissionId, PublisherConst.SubmissionPdfName,
            _objSubmission.SaveSubmissionPdf)
    };
    tasks.ForEach(t => t.ContinueWith(ExceptionHandler, TaskContinuationOptions.OnlyOnFaulted));
    tasks.SendEmailTasks(u => SendRegenerateSubmissionPdfEmail(submissionId, u));
}

private void ExceptionHandler(Task task)
{
    // Handle exception
}

After you create and start the tasks in your list you assign each of them a continuation in case an exception is thrown. But based on your code I can't see the need for a list of Tasks. You could simply use the task object and assign it the continuation.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
Ninglin
  • 146
  • 1
  • 9
0

As I understand, SendEmailTasks is an extension method, but you can simply use only one task, like this:

private void RegenerateSubmissionPdf(int submissionId)
{
    var submissionPath = PublisherConfigurationManager.SubmissionPath + submissionId;

    var task = HttpContext.Current.GeneratePdfTask(submissionPath, submissionId, PublisherConst.SubmissionPdfName,
            _objSubmission.SaveSubmissionPdf);
    task.ContinueWith(t =>  
      SendRegenerateSubmissionPdfEmail(submissionId, task),  
      TaskContinuationOptions.OnlyOnRanToCompletion);
    task.ContinueWith(t =>  
      HandleException(task.Exception),  
      TaskContinuationOptions.OnlyOnFaulted);
    await task;
}

This approach will continue execution if there is no exception and will handle the exception if there is one. And consider using the await constructure or Run method instead of StartNew (great article about this by Stephen Cleary).

VMAtm
  • 27,943
  • 17
  • 79
  • 125
  • About list - i just make list of 1 element for this sample. In my app i use from 1 to 3 `Task` – demo Apr 14 '15 at 11:21