Skip to main content

Asynchronous Processing


If you call a DynamicPDF API endpoint multiple times, create large PDFs, or create PDFs require complex processing, consider using the endpoint's asynchronous process method.
info

Refer to each technology's documentation on asynchronous programming.

C# uses the Task-based Asynchronous Pattern (TAP) for asynchronous programming and provides the Task and Task<T> objects for asynchronous operations.

These objects use the async and await keywords. Refer to Microsoft's Asynchronous Programming with async and await for more details.

Asynchronous methods are provided for the following endpoint classes.

  • DlexLayout
public Task<PdfResponse> ProcessAsync();
Source: DlexLayout.cs
  • ImageInfo
public Task<ImageResponse> ProcessAsync();
Source: ImageInfo.cs
  • Pdf
public Task<PdfResponse> ProcessAsync();
Source: Pdf.cs
  • PdfInfo
public Task<PdfInfoResponse> ProcessAsync();
Source: PdfInfo.cs
  • PdfText
public Task<PdfTextResponse> ProcessAsync();
Source: PdfText.cs
  • PdfXmp
public Task<XmpResponse> ProcessAsync();
Source: PdfXmp.cs
tip

Refer to Asynchronous Processing documentation in Microsoft's C# Users Guide. Also refer to Asynchronous Request-Reply Pattern or Asynchronous Programming with async and await for more details.

Example

The following example illustrates using the ProcessAsync method with the dlex-layout endpoint (DlexLayout class). It is not production code, but illustrates the asynchronous nature when using the ProcessAsync method.

using DynamicPDF.Api;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace AsyncExample
{
class Program
{
static void Main(string[] args)
{
Run("<api-key>", "c:/temp/");
}

static void Run(String apiKey, String basePath)
{
Task[] tasks = new Task[20];

for (int i = 0; i < 20; i++)
{
String user = "user" + i;
tasks[i] = RunAsync(apiKey, basePath, user).ContinueWith(x=>SaveFile(x.Result, basePath, user));
}
Task.WaitAll(tasks);
}

static Task<PdfResponse> RunAsync(String apiKey, String basePath, String userId)
{

LayoutDataResource layoutData = new LayoutDataResource(basePath + "report-with-cover-page.json");
DlexLayout dlexEndpoint = new DlexLayout("samples/report-with-cover-page/report-with-cover-page.dlex", layoutData);
dlexEndpoint.ApiKey = apiKey;
return dlexEndpoint.ProcessAsync();
}

static void SaveFile(PdfResponse response, string basePath, string userId)
{
if (response.IsSuccessful)
{
File.WriteAllBytes(basePath + userId + "-output.pdf", response.Content);
}
else
{
Console.WriteLine("ErrorId: " + response.ErrorId);
Console.WriteLine("ErrorMessage: " + response.ErrorMessage);
Console.WriteLine("ErrorJson: " + response.ErrorJson);
}
}
}
}
Source: AsynExample.cs

The important detail is the following code.

RunAsync(apiKey, basePath, user).ContinueWith(x=>SaveFile(x.Result, basePath, user));

The RunAsync method calls the ProcessAsync method and returns a Task<PdfResponse>. This call ensures each call to the endpoint is asynchronous. When RunAsync completes, it passes the PdfResponse to the SaveFile method to save the file to disk. The code Task.WaitAll(tasks); is a convenience so that the main program awaits completion (it's not production code).

   Follow us on social media for latest news!