我有个问题。我需要处理某些文件夹中的文件并将所有文件提取到一个文件夹中。
我的问题是线程丢失或无法正常工作。
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;
}
}