我目前正在处理项目,其中应用程序从文件夹中获取文件并到达每个图像以获取条形码。当它读取条形码时,它会推送数组中的所有非条形码文件,并且该文件数组将与目标中具有唯一名称的多页单个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();
}
答案 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();
}
}
}
}