C# 如何在我的项目中使用多线程?

C# 如何在我的项目中使用多线程?,c#,ocr,C#,Ocr,我正在用C#练习一个OCR程序,我不是一个很好的编码员,所以我试图找到自己的方法 1-IOCR一些pdf文件 2-我看到了OCR的输出 3-我使用UI按钮浏览,然后单击转换 4-我在UI上有一个进度条,但它没有可视化升级,而当我记录progressBar.Value时,我看到它的编号正在更新 所以我四处搜索,发现问题是我应该停止线程并创建一个新的线程,以便Ui进行可视化更新,但我真的不明白这一点,甚至不知道如何做 有人能帮我吗?像婴儿步 我也知道我已经复制和粘贴了很多代码供您查看 情况如下: 1

我正在用C#练习一个OCR程序,我不是一个很好的编码员,所以我试图找到自己的方法

1-IOCR一些pdf文件

2-我看到了OCR的输出

3-我使用UI按钮浏览,然后单击转换

4-我在UI上有一个进度条,但它没有可视化升级,而当我记录
progressBar.Value
时,我看到它的编号正在更新

所以我四处搜索,发现问题是我应该停止线程并创建一个新的线程,以便Ui进行可视化更新,但我真的不明白这一点,甚至不知道如何做

有人能帮我吗?像婴儿步

我也知道我已经复制和粘贴了很多代码供您查看

情况如下:

1-类
fmMain
Form
具有
progressbarincrementiation
函数,负责从
processFunctions
类中的函数获取增量值

2-
progressbarincrementiation
函数有
progressBar.Value
要更新,我看到它的值更新了

3-但视觉上没有任何更新。我尝试了一些线程代码,但我不太理解

class processFunctions 
    {
        Thread newThread = Thread.CurrentThread;
        private string invoiceNameIndex = "";
        private string invoiceIBANIndex = "";
        private string invoiceNumberIndex = "";
        private string invoiceDateIndex = "";
        private string invoiceSubtotalIndex = "";
        private string invoiceVATIndex = "";
        private string invoiceTotalIndex = "";
        private string[] filePath;
        private string[] fileNamePDF;
        private int totalNumberOfFiles;

        private string InformationNeeded(string wholeRead, string ix)
        {

            string[] lines = wholeRead.Split(new[] { "\r\n", "\r", "\n", " " }, StringSplitOptions.None);
            if(ix.Contains(","))
            {
                string[] variableIndex = ix.Split(',');
                string name = "";
                for(int i =0; i < variableIndex.Length; i++)
                {
                    name += lines[Convert.ToInt32(variableIndex[i])]; 
                }
                return name;
            }

            return lines[Convert.ToInt32(ix)];
        }

        public void ocrFunction(string filePathOnly)
        {

            var Ocr = new AutoOcr();
            var Results = Ocr.ReadPdf(filePathOnly);
            var Barcodes = Results.Barcodes;
            var Text = Results.Text;
            string[] numbers = { invoiceNameIndex, invoiceIBANIndex, invoiceNumberIndex,
                invoiceDateIndex, invoiceSubtotalIndex, invoiceVATIndex, invoiceTotalIndex};
            string[] results = new string[numbers.Count()];

            for (int i = 0; i < numbers.Length; i++)
            {
                results[i] = InformationNeeded(Text, numbers[i]);
                Console.WriteLine(results[i]);
            }
            Results = null;
            Ocr = null;
            Barcodes = null;
            Text = null;

        }

        public int browseFile()
        {
            Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
            OpenFileDialog ofd = new OpenFileDialog();
            int numberOfFilesToBeProcessed = 0;
            ofd.Filter = "PDF|*.pdf";
            ofd.Multiselect = true;
            string[] name = new string[2];

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                numberOfFilesToBeProcessed = ofd.FileNames.Length;
                filePath = ofd.FileNames;
                fileNamePDF = ofd.SafeFileNames;
            }
            this.totalNumberOfFiles = ofd.FileNames.Length;
            return numberOfFilesToBeProcessed;
        }

        public void databaseReader()
        {

            string connectionString;
            SqlConnection connection;
            connectionString = ConfigurationManager.ConnectionStrings["OCR_App.Properties.Settings.LibraryConnectionString"].ConnectionString;


            for (int i = 0; i < fileNamePDF.Length; i++)
            {

                string fileNameFiltered = fileNamePDF[i].Replace(".pdf", "");

                using (connection = new SqlConnection(connectionString))
                using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM invoicesTable WHERE invoiceRef = '" + fileNameFiltered + "'", connection))
                {
                    DataTable invoicesTable = new DataTable();
                    adapter.Fill(invoicesTable);

                    DataRow index = invoicesTable.Rows[0];

                    invoiceNameIndex = (index[1].ToString());
                    invoiceIBANIndex = (index[2].ToString());
                    invoiceNumberIndex = (index[3].ToString());
                    invoiceDateIndex = (index[4].ToString());
                    invoiceSubtotalIndex = (index[5].ToString());
                    invoiceVATIndex = (index[6].ToString());
                    invoiceTotalIndex = (index[7].ToString());

                    ocrFunction(filePath[i]);
                    //newThread.Start();
                    fmMain formFunctions = new fmMain();
                    //Thread.Yield();

                    //Thread thread = new Thread(() => formFunctions.ProgressBarIncrementation(progressBarIncrement()));
                    formFunctions.ProgressBarIncrementation(progressBarIncrement());

                }
            }
        }
        public int progressBarIncrement()
        {
            int incrementValue = 0;
            incrementValue = incrementValue + 100 / totalNumberOfFiles;
            //Console.WriteLine(incrementValue);
            return incrementValue;
        }
///////////////////////////////////////////////////////////////////
public partial class fmMain : Form
    {
        processFunctions processingMain = new processFunctions();
        ProgressBar NewprogressBar = new ProgressBar();
        private static int incrementbar = 0;

        public fmMain()
        {
            InitializeComponent();
        }

        [STAThread]
        private void BtnBrowse_Click(object sender, EventArgs e)
        //Browse the file needed to scan.
        {            
            int number = processingMain.browseFile();
            txtBoxFilePath.Text = number.ToString();
        }

        public void BtnConvert_Click(object sender, EventArgs e)
        {
            processingMain.databaseReader(); 
        }

        private void fmMain_Load(object sender, EventArgs e)
        {
        }

        private void txtBoxFilePath_TextChanged(object sender, EventArgs e)
        { 
        }

        private void NumberOfFilesToBeProcessed_Click(object sender, 
        EventArgs e)
        {
        }

        public void progressBar_Click(object sender, EventArgs e)
        {

            progressBar.Maximum = 100;
            NewprogressBar.Value = progressBar.Value;

        }
        public void ProgressBarIncrementation(int incrementValue)
        {
            //Thread.Yield();
            //Thread newThread = Thread.CurrentThread;

            incrementbar = incrementbar + incrementValue;
            progressBar.Visible = true;
            progressBar.Value += incrementbar;
            Thread thread = new Thread(() => progressBar.Value += incrementbar);
            Console.WriteLine(progressBar.Value);


            //progressBar.Value += incrementbar;

        }
    }
类处理函数
{
线程newThread=Thread.CurrentThread;
私有字符串invoiceNameIndex=“”;
私有字符串invoiceBanIndex=“”;
私有字符串invoiceNumberIndex=“”;
私有字符串invoiceDateIndex=“”;
私有字符串invoiceSubtotalIndex=“”;
私有字符串invoicevantindex=“”;
私有字符串invoiceTotalIndex=“”;
私有字符串[]文件路径;
私有字符串[]文件名PDF;
私有int totalNumberOfFiles;
需要私有字符串信息(字符串全称、字符串ix)
{
string[]line=wholeRead.Split(新[]{“\r\n”、“\r”、“\n”、“}”,StringSplitOptions.None);
如果(ix.Contains(“,”))
{
字符串[]variableIndex=ix.Split(',');
字符串名称=”;
for(int i=0;i