如何使用異步和多執行緒生成PDF
在批量生成或優化效能時,非同步和多執行緒非常有用,尤其是在使用 IronPDF 在 C# 和 VB.NET 中生成高效能 PDF時。
開始使用 IronPDF
立即在您的專案中使用IronPDF,並享受免費試用。
如何使用異步和多執行緒生成PDF
異步示例
IronPDF 完全支援非同步,透過其非同步渲染方法,例如 RenderHtmlAsPdfAsync 方法。
: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 秒的延遲,使用WaitFor 類別來模擬複雜的 HTML 渲染。 以下是使用上述各種技術的性能比較表。
| Normal Render | Asynchronous Render | Multithreaded Render | 
|---|---|---|
| 15.75 seconds | 05.59 seconds | 05.68 seconds | 

										
            
            
            
          
              