C# 多线程,每个线程按顺序运行,以生成具有唯一文件名的多个tiff图像

C# 多线程,每个线程按顺序运行,以生成具有唯一文件名的多个tiff图像,c#,C#,我目前正在工作的项目中的应用程序从文件夹中获取文件,并达到每个条码图像。当它读取条形码时,会将所有非条形码文件推送到数组中,该文件数组将与目标中具有唯一名称的多页单tiff图像合并。 我所做的一切和它的工作,但我想让它快速使用线程。 我有这样的功能- 读取图像文件,直到到达条形码 在目标文件夹中创建唯一的名称 制作多页单tiff图像 将其保存在目标文件夹中 为了读取条形码,我使用了简单的条形码阅读器,在这里我可以得到带有条形码或否的图像,其余的我用foreach循环中的数组来完成。这里有一些代码

我目前正在工作的项目中的应用程序从文件夹中获取文件,并达到每个条码图像。当它读取条形码时,会将所有非条形码文件推送到数组中,该文件数组将与目标中具有唯一名称的多页单tiff图像合并。 我所做的一切和它的工作,但我想让它快速使用线程。 我有这样的功能-

  • 读取图像文件,直到到达条形码
  • 在目标文件夹中创建唯一的名称
  • 制作多页单tiff图像
  • 将其保存在目标文件夹中
  • 为了读取条形码,我使用了简单的条形码阅读器,在这里我可以得到带有条形码或否的图像,其余的我用foreach循环中的数组来完成。这里有一些代码

     foreach (string path in filenames_1)
    
                {
    
    
                        Image bmp = Bitmap.FromFile(path);
                        Bitmap bn = (Bitmap)bmp;
                        readimage(bn);
    
                        if (s == 1) //If it is Bookmarked Content Found Then s=1 else its s=0
                        {
                            if (z == 0) // i used z coz in my process first file in directory is barcode image so first time have to skip it so i set z=1 at begining and second time it become 0 and process continues
                            {
                                j = 3; continue;
                                MakeUnique("c:\\sachin.tiff");
                                ConvertToMultiPageTiff(fileNames, fnamem);
                                fileNames.Clear();
    
    
                            }
                            fileNames.Add(path);
                            j = 1;
                            if (z == 0)
                            {
                                j = 3;
    
                            }
                            else
                            {
    
                            }
    
                        }
                        else
                        {
    
                            if (j == 1) // j==1 means its regular tiff file wher i make them add to string array
                            {
                                fileNames.Add(path); // string array with files to be made multiple tiff image become single mulipage tiff image
                                j = 1;
                                z = 0;
                            }
    
                            if (j == 3)
                            {
                                z = 1;
                                j = 1;
                                fileNames.Add(path);
                                MakeUnique("c:\\sachin.tiff");
                                ConvertToMultiPageTiff(fileNames, fnamem); // this function converts all the added files in filearray of single tiff image to multiple page single tiff image
                                fileNames.Clear();
    
    
                            }
    
                            else
                            {
    
                            }
    
    
                        }
                    }
    
    
    
            }
    
            MakeUnique("c:\\sachin.tiff");
            ConvertToMultiPageTiff(fileNames, fnamem);
            fileNames.Clear();
         }
    
    试试这个逻辑

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Net;
    using System.Collections;
    using System.Threading;
    namespace ConsoleApplication4
    {
    
    
        static class Program
        {
            private static ManualResetEvent wh;
            private static int total;
            private static int done;
            private static void Main()
            {
    
                string[] myfiles = new string[] {"file1", "file2"};
                wh = new ManualResetEvent(false);
                total = myfiles.Length;
                foreach (string myfile in myfiles)
                {
                    ThreadPool.QueueUserWorkItem(ProcessImageFile, myfile);
                }
                wh.WaitOne(Timeout.Infinite);
    
                //wohoo all files are process now, at faster rate;
    
            }
    
            private static void ProcessImageFile(object state)
            {
                string file = state as string;
    
                // process the file here
    
                Interlocked.Increment(ref done);
    
                if (done == total)
                {
                    wh.Set();
                }
    
            }
        }
    
    }
    

    这很有可能是IO受限的操作。。。因此,“多线程”实际上会随着HD竞争的增加而减慢速度。WTF是
    s
    j
    z
    ,你能在一周内记住吗?如果你“按顺序”执行,使用多个线程没有任何好处。多线程的全部目的是同时做多件事情。“我想通过使用多个线程来加快速度,但我想一次只运行一个线程”是胡说八道;如果您一次运行一个线程,则不需要“更快”。它仍然是一次一个。兄弟,谢谢你的回复,但是当我在图像处理中使用u r逻辑进行调试时,就像一个文件只需要一行代码一样。不完整的代码。你能更具体一点吗?好的。。在u r logic中,“ProcessImageFile”方法中的“ThreadPool.QueueUserWorkItem(ProcessImageFile,myfile);”的每条指令的for部分。如果我像上面那样开始处理文件,它会从该方法中取出一行,然后从该方法中退出,并从该方法中取出另一行。。它无法完成该方法的整个过程。希望你能理解……但是所有队列都是并行运行的,试一下,不进行调试,而调试时你不会真正知道某个应用程序是否是多线程的。但它在读取条形码图像时出错,并表示不能传递空值。。