C# 如何做到两个线程不能访问同一个文件夹

C# 如何做到两个线程不能访问同一个文件夹,c#,multithreading,C#,Multithreading,我正在编写一个多线程应用程序,它是windows服务。我有20个文件夹。我在start方法上创建了15个线程。我想实现这一目标;15个线程依次转到文件夹1、2、3、…、15。当一个线程完成时,它会创建另一个线程。此创建的线程必须转到第16个文件夹。它不能转到工作文件夹。我该怎么做?也就是说,我如何才能确保两个线程不在同一个文件夹中?您是否可以使用一个静态变量作为文件夹名称的计数器 比如: private static int _folderNameCounter = 0; private sta

我正在编写一个多线程应用程序,它是windows服务。我有20个文件夹。我在start方法上创建了15个线程。我想实现这一目标;15个线程依次转到文件夹1、2、3、…、15。当一个线程完成时,它会创建另一个线程。此创建的线程必须转到第16个文件夹。它不能转到工作文件夹。我该怎么做?也就是说,我如何才能确保两个线程不在同一个文件夹中?

您是否可以使用一个静态变量作为文件夹名称的计数器

比如:

private static int _folderNameCounter = 0;
private static readonly object _padlock = new object();
public static int GetFolderCounter()
{
     lock(_padlock)
     {
         _folderNameCounter++;
         return _folderNameCounter;
     }
}

public static void Main()
{
        for(int i = 0; i < 20; i++)
        {

            Task.Factory.StartNew(() => 
             {
                var path = @"c:\temp\" + GetFolderCounter();
                Directory.CreateDirectory(path);
                // add your own code for the thread here
             });
        }

}
private static int\u foldernameconter=0;
私有静态只读对象_挂锁=新对象();
公共静态int GetFolderCounter()
{
锁(挂锁)
{
_folderNameCounter++;
返回folderNameCounter;
}
}
公共静态void Main()
{
对于(int i=0;i<20;i++)
{
Task.Factory.StartNew(()=>
{
var path=@“c:\temp\”+GetFolderCounter();
CreateDirectory(路径);
//在此处添加您自己的线程代码
});
}
}
注意:我使用了,而不是直接使用线程,因为我认为。当然,您可以有特定的需求,这意味着线程是更好的解决方案 你的案子

使用a并用文件夹编号填充集合。每个任务处理集合的一个项,集合本身处理多线程方面,因此每个项仅由一个使用者处理

// Define the blocking collection with a maximum size of 15.
const int maxSize = 15;
var data = new BlockingCollection<int>(maxSize);

// Add the data to the collection.
// Do this in a separate task since BlockingCollection<T>.Add()
// blocks when the specified capacity is reached.
var addingTask = new Task(() => {
    for (int i = 1; i <= 20; i++) {
        data.Add(i);
    }
).Start();

// Define a signal-to-stop bool
var stop = false;

// Create 15 handle tasks.
// You can change this to threads if necessary, but the general idea is that
// each consumer continues to consume until the stop-boolean is set.
// The Take method returns only when an item is/becomes available.
for (int t = 0; t < maxSize; t++) {
    new Task(() => {
        while (!stop) {
            int item = data.Take();
            // Note: the Take method will block until an item comes available.
            HandleThisItem(item);
        }
    }).Start();
};

// Wait until you need to stop. When you do, set stop true
stop = true;
//定义最大大小为15的阻塞集合。
常量int maxSize=15;
var数据=新的BlockingCollection(maxSize);
//将数据添加到集合中。
//在BlockingCollection.Add()之后的单独任务中执行此操作
//达到指定容量时阻塞。
var addingTask=新任务(()=>{
对于(int i=1;i{
当(!停止){
int item=data.Take();
//注意:Take方法将一直阻止,直到项目可用。
手柄该项目(项目);
}
}).Start();
};
//等待直到需要停止。当需要停止时,将“停止”设置为true
停止=真;

你的意思是每15个文件夹有15个线程,还是每个线程有一个文件夹。如果是后者,为什么需要线程来实现同步?我的意思是每个线程有一个文件夹。只需传递一个参数来确定数量。或者使用ParallelforCreate 15个线程是不可能并行的。因为我想。是的,这可能是我的任务解决方案实际上也不能在15个线程上运行。默认情况下,任务使用线程池,该线程池的线程数有限(尽管您可以设置自己的最大线程数)。无论如何,即使您自己创建了线程,您也可以为文件夹名称设置一个静态计数器。不,它实际上不会启动20个线程。taskscheduler将确定有多少线程。如果“明确”要启动20个线程,您可以将TaskCreationOptions.LongRunning选项指定为Task.Factory.StartNew,(似乎并不能100%保证您获得那么多线程):