我有问题。我需要处理某些文件夹中的文件并将所有文件提取到一个文件夹。
我的问题是线程丢失或无法正常工作。
Product lstProduct = new Product();
lstProduct = XML.LoadProducts();
List<Thread> threads = new List<Thread>();
for (int i = 0; i < lstProduct.Length; i++)
{
Thread thread = new Thread(new ParameterizedThreadStart(ExtractFiles));
thread.SetApartmentState(ApartmentState.STA);
threads.Add(thread);
}
for (int i = 0; i < threads.Count; i++)
{
threads[i].Start(produtosConfiguracao[i]);
}
调用功能:
public void ExtractFiles(object config)
{
Product product= (Product)config;
try
{
ThreadPool.SetMaxThreads(Int32.Parse(product.Threads), Int32.Parse(product.Threads));
DirectoryInfo di = new DirectoryInfo(product.Path);
List<FileInfo> lstFiles= di.GetFiles(product.Type).Where(o => o.Extension == ".zip").ToList();
foreach (FileInfo fileInfo lstFiles)
{
lock(this)
{
FileProduct fileProduct = new FileProduct();
fileProduct.File= fileInfo;
fileProduct.Product = product;
ThreadPool.QueueUserWorkItem(new WaitCallback(ExtractAndMove), fileProduct);
}
}
catch (Exception ex)
{
throw;
}
}
答案 0 :(得分:0)
如果可以,您应该考虑使用I / O函数的* Async重载并在4.5中使用async / await。您正尝试在此处执行操作系统通过IOCP为您执行的操作。