非同期およびマルチスレッドを使用してPDFを生成する方法
非同期とスレッディングは、以下を生成するときに便利です。IronPdfを使ってC#とVB.NETで高性能PDFを作成するバッチまたは最適化されたパフォーマンス
IronPDFを始めましょう
今日から無料トライアルでIronPDFをあなたのプロジェクトで使い始めましょう。
非同期およびマルチスレッドを使用してPDFを生成する方法
非同期の例
IronPDFは、RenderHtmlAsPdfAsync
メソッドなどのAsyncレンダリングメソッドを使用してAsyncを完全にサポートしています。
:path=/static-assets/pdf/content-code-examples/how-to/async-async.cs
using IronPdf;
using System.Threading.Tasks;
// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
string[] htmlStrings = {"<h1>Html 1</h1>", "<h1>Html 2</h1>", "<h1>Html 3</h1>"};
// Create an array to store the tasks for rendering
var renderingTasks = new Task<PdfDocument>[htmlStrings.Length];
for (int i = 0; i < htmlStrings.Length; i++)
{
int index = i; // Capturing the loop variable
renderingTasks[i] = Task.Run(async () =>
{
// Render HTML to PDF
return await renderer.RenderHtmlAsPdfAsync(htmlStrings[index]);
});
}
// Wait for all rendering tasks to complete
// await Task.WhenAll(renderingTasks);
Imports IronPdf
Imports System.Threading.Tasks
' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()
Private htmlStrings() As String = {"<h1>Html 1</h1>", "<h1>Html 2</h1>", "<h1>Html 3</h1>"}
' Create an array to store the tasks for rendering
Private renderingTasks = New Task(Of PdfDocument)(htmlStrings.Length - 1){}
For i As Integer = 0 To htmlStrings.Length - 1
Dim index As Integer = i ' Capturing the loop variable
renderingTasks(i) = Task.Run(Async Function()
' Render HTML to PDF
Return Await renderer.RenderHtmlAsPdfAsync(htmlStrings(index))
End Function)
Next i
' Wait for all rendering tasks to complete
' await Task.WhenAll(renderingTasks);
マルチスレッドの例
IronPDFはスレッドセーフであり、IronPdf.ChromePdfRendererレンダリングエンジンを使用する際にはマルチスレッドをサポートします。
制限の一つとして、macOSマシンではマルチスレッドが制限されています。
Parallel.ForEach
パターンはPDFのバッチ処理に非常に役立つことが分かりました。
:path=/static-assets/pdf/content-code-examples/how-to/async-multi-thread.cs
using IronPdf;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
var queue = new List<string>() { "<h1>Html 1</h1>", "<h1>Html 2</h1>", "<h1>Html 3</h1>" };
// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Create a list to store the rendered PDFs
List<PdfDocument> pdfResults = new List<PdfDocument>();
Parallel.ForEach(queue, html =>
{
// Render HTML to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
// You may choose to save the PDF to disk here if needed
// For this example, we'll store it in the pdfResults list
lock (pdfResults)
{
pdfResults.Add(pdf);
}
});
Imports IronPdf
Imports System.Collections.Concurrent
Imports System.Collections.Generic
Imports System.Threading.Tasks
Private queue = New List(Of String)() From {"<h1>Html 1</h1>", "<h1>Html 2</h1>", "<h1>Html 3</h1>"}
' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()
' Create a list to store the rendered PDFs
Private pdfResults As New List(Of PdfDocument)()
Parallel.ForEach(queue, Sub(html)
' Render HTML to PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)
' You may choose to save the PDF to disk here if needed
' For this example, we'll store it in the pdfResults list
SyncLock pdfResults
pdfResults.Add(pdf)
End SyncLock
End Sub)
パフォーマンス比較
比較を行いましょう。 以下のようにレンダリングに5秒の遅延を追加しました複雑なHTMLレンダリングをシミュレートするWaitForクラス. 以下は、上記で説明したさまざまな技術を使用したパフォーマンスの比較表です。
通常レンダー | 非同期レンダー | マルチスレッドレンダリング |
---|---|---|
5.75秒 | 5.59秒 | 5.68秒 |