IronBarcode 操作指南 設定最大並行線程數 如何設定最大並行線程數 Curtis Chau 更新:2025年11月29日 下載 IronBarcode NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English 讀取大量條碼時,依賴單執行緒進程可能會造成效能瓶頸並限制可擴展性。 但是,使用平行執行緒可以讓應用程式同時處理多個影像,從而有效地提高總處理能力,並大幅縮短完成批次作業所需的時間。 為這些執行緒設定最大限制是優化效能的有效方法。 它透過在處理器核心之間平衡工作負載,確保應用程式充分利用硬體的潛力。 這種方法可以最大限度地提高效率,在保持應用程式流暢運行的同時,提供盡可能快的結果。 IronBarcode 提供了一種簡單的方法來控制此限制,從而確保最佳的機器性能。 以下部分示範如何輕鬆設定這些線程限制。 ## 如何在 C# 中設定最大並行線程數 下載 IronBarcode C# 函式庫,設定讀取條碼的最大平行執行緒數。 從指定資料夾載入條碼 實例化一個新的BarcodeReaderOptions對象 將Multithreaded設為 true,並為MaxParallelThreads提供一個整數值。 使用BarcodeResults列印並顯示結果 設定最大並行線程數 在這個例子中,我們將使用大量的條碼圖像來說明使用多執行緒進程而不是單執行緒進程的可擴展性和效率。 您可以點擊此處下載圖片資料夾。 要配置 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}"); } } Imports IronBarCode Imports System Imports System.IO Dim maxParallelThreads As Integer = 4 Dim optionsFaster As New BarcodeReaderOptions With { .Multithreaded = True, .MaxParallelThreads = maxParallelThreads } ' Dynamically get the "images" folder in the current directory Dim folderPath As String = Path.Combine(Directory.GetCurrentDirectory(), "images") ' Retrieve all JPG files in the directory Dim pdfFiles = Directory.GetFiles(folderPath, "*.jpg") For Each file In pdfFiles ' Read the barcode Dim results = BarcodeReader.Read(file) For Each result In results ' Show the type and value for every barcode found Console.WriteLine($"Value: {result.Value}, Type: {result.BarcodeType}") Next Next $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"); Imports IronBarCode Imports System Imports System.Diagnostics Imports System.IO Imports System.Linq ' Set the max parallel threads to the number of processor cores Dim maxParallelThreads As Integer = Environment.ProcessorCount Dim optionsFaster As New BarcodeReaderOptions With { .Multithreaded = True, .MaxParallelThreads = maxParallelThreads, .ExpectMultipleBarcodes = True } ' Start timing the process Dim stopwatch As Stopwatch = Stopwatch.StartNew() ' Dynamically get the "images" folder in the current directory Dim folderPath As String = Path.Combine(Directory.GetCurrentDirectory(), "images") ' Check if directory exists to prevent crashes If Not Directory.Exists(folderPath) Then Console.WriteLine($"Error: The directory '{folderPath}' does not exist.") Return End If ' Get all JPG files in the directory Dim pdfFiles As String() = Directory.GetFiles(folderPath, "*.jpg") For Each file As String In pdfFiles ' Read the barcode Dim results = BarcodeReader.Read(file) If results.Any() Then Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}") For Each result In results Console.WriteLine($" Value: {result.Value}, Type: {result.BarcodeType}") Next End If Next 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線程處理時間 過程的處理時間為 84 秒。 處理時間與環境處理器數量 如您所見,此操作的處理時間為 53 秒,這比僅使用四個執行緒運行它要快得多。 但請注意,使用更多執行緒並不能保證效能提升,因為效能取決於主機處理器。 一般經驗法則是使用可用處理器的最大數量減一,以確保仍有一個執行緒可用於其他系統操作。 專案環境必須配置為允許多執行緒。 否則,將Multithreaded設為true並增加MaxParallelThreads值並不會提高進程速度,反而可能會降低進程速度。 常見問題解答 在 IronBarcode 中設定最大並行執行緒數的目的是什麼? 設定最大平行執行緒數可以有效利用系統資源,從而優化條碼產生效能,尤其是在批次處理大量條碼時。 如何在IronBarcode中配置最大並行執行緒數? 您可以透過在 C# 程式碼中使用適當的方法來配置 IronBarcode 中的最大並行執行緒數,從而設定條碼產生任務所需的執行緒數。 為什麼優化批量條碼創建的效能很重要? 優化大量條碼建立的效能,可確保流程高效快速,減少產生大量條碼所需的時間和資源,這對於高吞吐量需求的應用至關重要。 在IronBarcode中使用平行處理有哪些好處? IronBarcode 的平行處理功能利用多個 CPU 核心,可更快地產生條碼,從而提高應用程式效能,並減少大規模條碼任務的處理時間。 設定過多的並行線程會對效能產生負面影響嗎? 是的,設定過多的平行執行緒會導致資源爭用和開銷增加,從而可能降低效能。找到一個與系統效能相符的平衡配置至關重要。 選擇並行執行緒數時應考慮哪些因素? 需要考慮的因素包括可用 CPU 核心數、系統負載以及條碼產生任務的性質。最好嘗試不同的設置,找到最佳配置。 IronBarcode 中並行執行緒是否有預設設定? IronBarcode 可能對並行線程有預設設置,但建議根據您的特定應用程式需求自訂此設置,以獲得最佳效能。 IronBarcode是如何處理執行緒管理的? IronBarcode 利用 .NET 的執行緒功能來管理平行處理,讓開發人員指定執行緒數以有效地最佳化效能。 我可以在執行過程中動態變更並行執行緒數嗎? 不建議在執行過程中動態變更並行執行緒數,因為這可能會導致結果不一致並影響效能穩定性。最好在開始條碼產生過程之前設定好配置。 條碼產生中設定最大並行執行緒數的常見應用場景有哪些? 常見用例包括需要高速產生條碼的應用,例如庫存管理系統、零售銷售點系統和物流應用,在這些應用程式中,快速處理大量條碼至關重要。 Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 準備好開始了嗎? Nuget 下載 2,070,733 | 版本: 2026.2 剛剛發布 免費 NuGet 下載 總下載量:2,070,733 查看許可證