多个线程与每个线程顺序运行,以使多个tiff图像具有唯一的文件名

时间:2012-12-29 02:35:51

标签: c#

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

这样的功能
  1. 读取图像文件,直到达到条形码。
  2. 在目标文件夹中创建唯一名称
  3. 制作多页单张图片
  4. 将其保存在目的地foler。
  5. 阅读条形码我使用简单的条形码阅读器,我得到图像有条形码或没有。休息我在做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();
         }
    

1 个答案:

答案 0 :(得分:0)

尝试这个逻辑

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();
            }

        }
    }

}