Skip to content

多线程环境报错 #179

@meloht

Description

@meloht

Describe the bug

多线程环境执行抛异常
System.Runtime.InteropServices.SEHException: 'External component has thrown an exception.'

Image

Steps to reproduce the bug

重现步骤与环境
1 paddleOCRAll 初始化定义在winform 构造函数

Image
  1. 在异步线程task 中执行 OCR 检测
Image
  1. 在form UI 中调用OCR 检测
Image

4 .在UI 线程与 task 线程中使用同一个paddleOCRAll 实例就会出异常

Image Image

Expected behavior

No response

Screenshots

Image

Release version

PaddleSharp 3.0.1

IDE

Visual Studio 2026/ .net 10

OS version

win10

Additional context

source code

using OpenCvSharp;
using Sdcb.PaddleInference;
using Sdcb.PaddleOCR;
using Sdcb.PaddleOCR.Models.Local;

namespace WinFormsOCRTest
{
    public partial class Form1 : Form
    {
        PaddleOcrAll _all;
        public Form1()
        {
            InitializeComponent();
            _all = new PaddleOcrAll(LocalFullModels.EnglishV4, PaddleDevice.Mkldnn());
            _all.AllowRotateDetection = false;
            _all.Enable180Classification = false;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Image Files|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff|All files|*.*";
            openFileDialog1.Title = "Select an image file";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBoxPath.Text = openFileDialog1.FileName;
            }
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = string.Empty;
            if (this.textBoxPath.Text == string.Empty)
            {
                MessageBox.Show("Please select an image file first.");
                return;
            }

            using Mat src = Cv2.ImRead(this.textBoxPath.Text) ;
            PaddleOcrResult result = _all.Run(src);
            this.textBox1.Text = result.Text.ToString();
        }

        private void btnTask_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = string.Empty;
            if (this.textBoxPath.Text == string.Empty)
            {
                MessageBox.Show("Please select an image file first.");
                return;
            }

            Task.Run(() =>
            {
                using Mat src = Cv2.ImRead(this.textBoxPath.Text);
                PaddleOcrResult result = _all.Run(src);
                this.Invoke(new Action(() =>
                {
                    this.textBox1.Text = result.Text.ToString();
                }));
            });
        }
    }
}

WinFormsOCRTest.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions