如何設定最大並行線程數

Curtis Chau
Curtis Chau
已更新: 2026年7月21日

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

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

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



設定最大並行線程數

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

要配置 IronBarcode 使用多於一個執行緒,首先需要實例化一個新的 BarcodeReaderOptions 物件,並將 Multithreaded 設定為 true。 之後,藉由指派整數值來設定 MaxParallelThreads 屬性。 預設情況下,MaxParallelThreads 設定為 4。

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

using Google.Protobuf.WellKnownTypes;
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, optionsFaster);

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

    }

}

輸出

多執行緒輸出

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

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

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

在此示例中,我們將展示上述相同的多執行緒情境,但加入計時器以比較使用 Environment.ProcessorCount 利用所有可用執行緒與預設值 4 的差異。 MaxParallelThreads 在我們的情況下,我們使用的是一台具有 32 個邏輯處理器的電腦,所以 Environment.ProcessorCount 將被設為 32。

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, optionsFaster);

    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");

輸出

4線程處理時間

4 處理器

在上述描述的 32 核機器上運行此示例時,此過程的處理時間約為 84 秒。 實際時間因硬體和工作負載而異。

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

32 處理器

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

警告: 項目環境必須配置以允許多執行緒。 否則,將 Multithreaded 設定為 true 並增加 MaxParallelThreads 將無法提高處理速度,反而可能會降低。

常見問題

如何通過設置最大平行線程來優化條碼生成性能?

設置平行線程的最大數量通過利用所有可用的處理器內核來優化性能,允許多個條碼圖像同時處理。這將增加處理能力並減少批量作業所需的時間。

您如何在C#中使用IronBarcode設置最大平行線程?

要在IronBarcode中設置最大平行線程,請實例化一個BarcodeReaderOptions物件,將Multithreaded設為true並為MaxParallelThreads分配一個整數值。此設置將決定執行條碼處理的線程數量。

IronBarcode中默認的最大平行線程數量是多少?

IronBarcode中MaxParallelThreads的默認值設為4。這個初始配置旨在確保在大多數多核心系統上高效處理。

MaxParallelThreads的值可以超過處理器內核的數量嗎?

雖然沒有硬性限制,但將MaxParallelThreads設置為超過可用邏輯核心數的值可能會因過多的上下文切換和開銷而降低性能。

設定MaxParallelThreads值時應考慮什麼?

在設置MaxParallelThreads時,請考慮您硬體中的邏輯處理器數量。最佳值可確保有效的多線程操作而不會使處理器過載,通常使用核心數減去一個以獲得最佳性能。

使用Environment.ProcessorCount設置MaxParallelThreads有什麼效果?

使用Environment.ProcessorCount將MaxParallelThreads設置為邏輯處理器的總數,使多核心機器上的線程資源得到充分利用,顯著提高處理時間。

硬體容量對多線程條碼處理有什麼影響?

硬體容量直接影響多線程的效能。邏輯核心的數量決定了線程的可行數量,平衡工作負載並避免資源限制導致的性能瓶頸。

為什麼增加MaxParallelThreads可能並不總能提高性能?

增加MaxParallelThreads可能會使CPU因上下文切換超載,尤其當其超過核心數時,導致報酬遞減,可能出現性能下降而不是提高。

如果範例代碼中指定的圖像目錄不存在會怎樣?

如果指定的目錄不存在,代碼會進行檢查,輸出一個錯誤信息並退出過程,以防止條碼讀取時崩潰。

確定最佳MaxParallelThreads值的建議方法是什麼?

建議在您的特定環境中測試不同的設置,考慮到系統資源和工作負載類型,以找到最大化性能的理想MaxParallelThreads值。

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 2,575,633版本:2026.11剛剛發布