【问题标题】:Multi threading with each thread run sequentially to make multiple tiff image with unique file name多线程,每个线程顺序运行以制作具有唯一文件名的多个 tiff 图像
【发布时间】:2012-12-14 05:35:09
【问题描述】:

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

  1. 读取图像文件直到到达条形码。
  2. 在目标文件夹中创建一个唯一的名称
  3. 制作多页单tiff图像
  4. 将其保存在目标文件夹中。

要读取条码,我使用简单的条码阅读器,我在其中得到图像有条码或没有条码。其余的我正在处理 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();
     }

【问题讨论】:

  • 很有可能这将是 IO 受限操作......因为随着 HD 竞争的增加,“多线程”实际上会减慢速度。 WTF 是sjz,你能在一周内记住吗?
  • 如果您“按顺序”执行此操作,则使用多个线程没有任何好处。多线程的全部目的是同时做多件事情。 “我想通过使用多个线程来更快地完成它,但我想一次只运行一个”是无稽之谈;如果您一次运行一个线程,则不涉及“更快”。还是一次一个。

标签: c#


【解决方案1】:

试试这个逻辑

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

        }
    }

}

【讨论】:

  • 兄弟感谢您的回复,但是正如我在处理图像时使用您的逻辑进行调试的那样,一个文件需要一行而不是完整的代码..
  • 你能再具体一点吗?
  • ok.. 在你的逻辑中,如果我像上面一样开始处理文件,则“ThreadPool.QueueUserWorkItem(ProcessImageFile,myfile);”的每条指令的部分在“ProcessImageFile”的方法中需要一行从该方法中退出并从该方法中退出并从中获取另一个文件。它不会完成该方法的完整过程。希望你能理解..
  • 但是所有启动的队列都是并行运行的,试试不带调试的,调试时你不会真正知道应用程序是否是多线程的
  • 我试过了。但它在读取条形码图像时出错并说不能传递空值..
猜你喜欢
  • 1970-01-01
  • 2019-05-19
  • 2018-10-01
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 2012-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多