如何划分的文件路径阵列分成几个较小的数组?几个、数组、阵列、较小

2023-09-06 17:19:40 作者:——野心

我看过几个不同的问题,从这么这个了,但是仍然没有能够得到它的工作。我使用公共静态字符串[]文件= Directory.GetFiles(currentDirectory所,* .WAV,SearchOption.AllDirectories); 来获取文件路径的数组会被传递到一个文件流。该操作FILESTREAM确实服用时间过长且只有一个线程处理所有的文件。所以,我决定我会分裂阵列,并通过那些规模较小的阵列不同的线程。

在code我是用了,我从另外一个SO问题了,并用它来传递拆分数组,但是它只是工作的第一阵列中只有一个文件,但我知道问题是什么:

  VAR的事情=从Enumerable.Range指数(0,files.Length)
          组文件[指数]通过索引/ 600;
的foreach(在事情VAR集)
    的string.join(;,set.ToArray());
 

(这不正是我怎么用它,我搞砸了这么多我不记得了。)这样做的问题是,一切都被视为只是一个巨大的文件的路径,我有一个foreach循环这会从较小的阵列中的每个文件,但它处理的每个文件作为只有一个,扔了filepathtoolong异常时,有一个从搜索多个文件返回。我的函数接受一个数组,然后使用的foreach(在smallerArray字符串文件)写每一个。我需要做的是打破了文件阵列分为4个更小的阵列,并开始喜欢新的线程新帖(()=> {的DoWork(newArray);})。开始(); ,但没有我试过工作过。

解决方案   

所以,我决定我会分裂阵列,并通过那些规模较小的阵列不同的线程。

听起来你做硬盘的方式:)让与框架为您处理它Parallel.ForEach:

  Parallel.ForEach(文件,文件=>
{
    //做的东西有一个文件
});
 

LabVIEW入门第九天 数组和簇

I've read several different questions from SO about this already but still haven't been able to get it to work. I'm using public static string[] files = Directory.GetFiles(CurrentDirectory, "*.wav", SearchOption.AllDirectories); to get an array of file paths that will then be passed of to a filestream. The operations the filestream does were taking too long with just one thread handling all of the files. So I decided I'd split the array and pass those smaller arrays to different threads.

The code I was using for that I got from another SO question, and used it to pass the split array, but it only worked with just one file in the first array, but I know what the problem was:

var thing = from index in Enumerable.Range(0, files.Length) 
          group files[index] by index/600;
foreach(var set in thing)
    string.Join(";", set.ToArray());

(This isn't exactly how I used it, I've messed with it so much I can't remember.) The problem with this is that everything was treated as just one massive file path, I have a foreach loop that gets each file from the smaller array, but it treated every file in it as just one, throwing the filepathtoolong exception when there was more than one file returned from the search. My function takes an array and then uses foreach (string file in smallerArray) to write to each one. What I need to do is break up the files array into 4 smaller arrays and start the new threads like new Thread(() => { DoWork(newArray); }).Start(); but nothing I've tried has worked.

解决方案

So I decided I'd split the array and pass those smaller arrays to different threads.

Sounds like you're doing it the hard way :) Let the framework handle it for you with Parallel.ForEach:

Parallel.ForEach(files, file => 
{
    // Do stuff with one file
});