如何設定最大並行線程數

This article was translated from English: Does it need improvement?
Translated
View the article in English

讀取大量條碼時,依賴單執行緒進程可能會造成效能瓶頸並限制可擴展性。 但是,使用平行執行緒可以讓應用程式同時處理多個影像,從而有效地提高總處理能力,並大幅縮短完成批次作業所需的時間。

為這些執行緒設定最大限制是優化效能的有效方法。 它透過在處理器核心之間平衡工作負載,確保應用程式充分利用硬體的潛力。 這種方法可以最大限度地提高效率,在保持應用程式流暢運行的同時,提供盡可能快的結果。

IronBarcode 提供了一種簡單的方法來控制此限制,從而確保最佳的機器性能。 以下部分示範如何輕鬆設定這些線程限制。

開始使用 IronBarcode



設定最大並行線程數

在這個例子中,我們將使用大量的條碼圖像來說明使用多執行緒進程而不是單執行緒進程的可擴展性和效率。 您可以點擊此處下載圖片資料夾。

要配置 IronBarcode 使用多個線程,首先需要實例化一個新的BarcodeReaderOptions對象,並將Multithreaded設為 true。 之後,透過賦予一個整數值來設定MaxParallelThreads屬性。 預設情況下, MaxParallelThreads設定為 4。

配置完成後,從資料夾匯入大量條碼影像。 然後,使用循環,透過Read方法讀取條碼圖像目錄,傳遞檔案路徑和配置的BarcodeReaderOptions 。 最後,透過存取BarcodeResults顯示條碼的值和類型。

:path=/static-assets/barcode/content-code-examples/how-to/set-max-parallel-thread.cs
using IronBarCode;
using System;
using System.IO;

int maxParallelThreads = 4;


var optionsFaster = new BarcodeReaderOptions
{
    // Set Max threads to 4
    Multithreaded = true,
    MaxParallelThreads = maxParallelThreads,
};

// Dynamically get the "images" folder in the current directory
string folderPath = Path.Combine(Directory.GetCurrentDirectory(), "images");

// Retrieve all JPG files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");

foreach (var file in pdfFiles)
{
    // Read the barcode
    var results = BarcodeReader.Read(file);

    foreach (var result in results)
    {
        // Show the type and value for every barcode found
        Console.WriteLine($"Value: {result.Value}, Type: {result.BarcodeType}");
            
    }
    
}

$vbLabelText   $csharpLabel

輸出

多執行緒輸出

如控制台輸出所示,它會顯示每個對應影像的條碼值和類型。

設定合適的最大平行執行緒數

Multithreaded屬性設定為 true 時, MaxParallelThreads屬性的預設值為 4。雖然MaxParallelThreads的整數值沒有硬性限制,但將其值設定得高於硬體的邏輯核心容量實際上會導致效能下降。 這是因為處理器無法處理過多的上下文切換,這可能會導致效能開銷而不是速度提升。 因此, MaxParallelThreads的正確值取決於電腦的規格,開發人員應該進行測試,以找到適合其環境的最佳值。

在這個範例中,我們將展示與上面相同的多執行緒場景,但會設定一個計時器,將預設值 4 與使用Environment.ProcessorCount來利用所有可用執行緒進行比較。 在本例中,我們使用一台具有 32 個邏輯處理器的計算機,因此MaxParallelThreads將設定為 32。

:path=/static-assets/barcode/content-code-examples/how-to/set-max-parallel-thread-performance.cs
using IronBarCode;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

// Set the max parallel threads to the number of processor cores
int maxParallelThreads = Environment.ProcessorCount;


var optionsFaster = new BarcodeReaderOptions
{
    // Set Max threads to the number of processor cores
    Multithreaded = true,
    MaxParallelThreads = maxParallelThreads,
    ExpectMultipleBarcodes = true,
};

// Start timing the process
var stopwatch = Stopwatch.StartNew();
// Dynamically get the "images" folder in the current directory
string folderPath = Path.Combine(Directory.GetCurrentDirectory(), "images");

// Check if directory exists to prevent crashes
if (!Directory.Exists(folderPath))
{
    Console.WriteLine($"Error: The directory '{folderPath}' does not exist.");
    return;
}

// Get all JPG files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");

foreach (var file in pdfFiles)
{
    // Read the barcode
    var results = BarcodeReader.Read(file);

    if (results.Any())
    {
        Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}");
        foreach (var result in results)
        {
            Console.WriteLine($"  Value: {result.Value}, Type: {result.BarcodeType}");
            
        }
    }
}

stopwatch.Stop();

// Print number of images the barcode reader could decode
Console.WriteLine($" Max parallel threads of {maxParallelThreads} with {stopwatch.Elapsed.TotalSeconds:F2}s");
$vbLabelText   $csharpLabel

輸出

4線程處理時間

4 處理器

過程的處理時間為 84 秒。

處理時間與環境處理器數量

32 處理器

如您所見,此操作的處理時間為 53 秒,這比僅使用四個執行緒運行它要快得多。 但請注意,使用更多執行緒並不能保證效能提升,因為效能取決於主機處理器。 一般經驗法則是使用可用處理器的最大數量減一,以確保仍有一個執行緒可用於其他系統操作。

專案環境必須配置為允許多執行緒。 否則,將Multithreaded設為true並增加MaxParallelThreads值並不會提高進程速度,反而可能會降低進程速度。

常見問題解答

在 IronBarcode 中設定最大並行執行緒數的目的是什麼?

設定最大平行執行緒數可以有效利用系統資源,從而優化條碼產生效能,尤其是在批次處理大量條碼時。

如何在IronBarcode中配置最大並行執行緒數?

您可以透過在 C# 程式碼中使用適當的方法來配置 IronBarcode 中的最大並行執行緒數,從而設定條碼產生任務所需的執行緒數。

為什麼優化批量條碼創建的效能很重要?

優化大量條碼建立的效能,可確保流程高效快速,減少產生大量條碼所需的時間和資源,這對於高吞吐量需求的應用至關重要。

在IronBarcode中使用平行處理有哪些好處?

IronBarcode 的平行處理功能利用多個 CPU 核心,可更快地產生條碼,從而提高應用程式效能,並減少大規模條碼任務的處理時間。

設定過多的並行線程會對效能產生負面影響嗎?

是的,設定過多的平行執行緒會導致資源爭用和開銷增加,從而可能降低效能。找到一個與系統效能相符的平衡配置至關重要。

選擇並行執行緒數時應考慮哪些因素?

需要考慮的因素包括可用 CPU 核心數、系統負載以及條碼產生任務的性質。最好嘗試不同的設置,找到最佳配置。

IronBarcode 中並行執行緒是否有預設設定?

IronBarcode 可能對並行線程有預設設置,但建議根據您的特定應用程式需求自訂此設置,以獲得最佳效能。

IronBarcode 如何處理執行緒管理?

IronBarcode 利用 .NET 的執行緒功能來管理平行處理,讓開發人員指定執行緒數以有效地最佳化效能。

我可以在執行過程中動態變更並行執行緒數嗎?

不建議在執行過程中動態變更並行執行緒數,因為這可能會導致結果不一致並影響效能穩定性。最好在開始條碼產生過程之前設定好配置。

條碼產生中設定最大並行執行緒數的常見應用場景有哪些?

常見用例包括需要高速產生條碼的應用,例如庫存管理系統、零售銷售點系統和物流應用,在這些應用程式中,快速處理大量條碼至關重要。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。

準備好開始了嗎?
Nuget 下載 2,035,202 | 版本: 2025.12 剛剛發布