Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何设置threadCounter变量的动态数量?_C#_Multithreading - Fatal编程技术网

C# 如何设置threadCounter变量的动态数量?

C# 如何设置threadCounter变量的动态数量?,c#,multithreading,C#,Multithreading,我不太喜欢多线程,所以这个问题可能很愚蠢,但我似乎找不到解决这个问题的方法(特别是因为我使用C#,而且我已经使用了一个月) 我有一个动态数量的目录(我从数据库中的查询中获得)。在这些查询中有一定数量的文件 对于每个目录,我需要使用一种方法以当前的方式使用FTP传输这些文件,因为我基本上没有FTP max连接的限制(不是我的话,它是在细节中写的) 但我仍然需要控制每个目录传输的最大文件量。因此,我需要计算正在传输的文件数(递增/递减) 我怎么做呢?我应该使用数组之类的东西并使用Monitor类吗

我不太喜欢多线程,所以这个问题可能很愚蠢,但我似乎找不到解决这个问题的方法(特别是因为我使用C#,而且我已经使用了一个月)

我有一个动态数量的目录(我从数据库中的查询中获得)。在这些查询中有一定数量的文件

对于每个目录,我需要使用一种方法以当前的方式使用FTP传输这些文件,因为我基本上没有FTP max连接的限制(不是我的话,它是在细节中写的)

但我仍然需要控制每个目录传输的最大文件量。因此,我需要计算正在传输的文件数(递增/递减)

我怎么做呢?我应该使用数组之类的东西并使用Monitor类吗

编辑:框架3.5

var allDirectories = db.GetAllDirectories();
foreach(var directoryPath in allDirectories)
{
    DirectoryInfo directories = new DirectoryInfo(directoryPath);
    //Loop through every file in that Directory
    foreach(var fileInDir in directories.GetFiles()) {
        //Check if we have reached our max limit
        if (numberFTPConnections == MAXFTPCONNECTIONS){
            Thread.Sleep(1000);
        }

        //code to copy to FTP
        //This can be Aync, when then transfer is completed
        //decrement the numberFTPConnections so then next file can be transfered.
     }
 }
你可以按照上面的思路试试。请注意,这只是基本逻辑,可能有更好的方法来做到这一点


你可以按照上面的思路试试。请注意,这只是基本逻辑,可能有更好的方法来做到这一点

您可以使用
信号量
类来限制每个目录中并发文件的数量。您可能希望每个目录有一个信号量,这样每个目录的FTP上载数量就可以独立控制

public class Example
{
  public void ProcessAllFilesAsync()
  {
    var semaphores = new Dictionary<string, Semaphore>();
    foreach (string filePath in GetFiles())
    {
      string filePathCapture = filePath; // Needed to perform the closure correctly.
      string directoryPath = Path.GetDirectoryName(filePath);
      if (!semaphores.ContainsKey(directoryPath))
      {
        int allowed = NUM_OF_CONCURRENT_OPERATIONS;
        semaphores.Add(directoryPath, new Semaphore(allowed, allowed));
      }
      var semaphore = semaphores[directoryPath];
      ThreadPool.QueueUserWorkItem(
        (state) =>
        {
          semaphore.WaitOne();
          try
          {
            DoFtpOperation(filePathCapture);
          }
          finally
          {
            semaphore.Release();
          }
        }, null);
    }
  }
}
公共类示例
{
public void processAllFileAsync()
{
var信号量=新字典();
foreach(GetFiles()中的字符串文件路径)
{
string filePathCapture=filePath;//需要正确执行闭包。
字符串directoryPath=Path.GetDirectoryName(filePath);
if(!semaphores.ContainsKey(directoryPath))
{
int allowed=并发操作的数量;
Add(directoryPath,new信号量(allowed,allowed));
}
var信号量=信号量[directoryPath];
ThreadPool.QueueUserWorkItem(
(州)=>
{
WaitOne()信号量;
尝试
{
doftpopulation(filePathCapture);
}
最后
{
semaphore.Release();
}
},空);
}
}
}

您可以使用
信号量
类来限制每个目录中并发文件的数量。您可能希望每个目录有一个信号量,这样每个目录的FTP上载数量就可以独立控制

public class Example
{
  public void ProcessAllFilesAsync()
  {
    var semaphores = new Dictionary<string, Semaphore>();
    foreach (string filePath in GetFiles())
    {
      string filePathCapture = filePath; // Needed to perform the closure correctly.
      string directoryPath = Path.GetDirectoryName(filePath);
      if (!semaphores.ContainsKey(directoryPath))
      {
        int allowed = NUM_OF_CONCURRENT_OPERATIONS;
        semaphores.Add(directoryPath, new Semaphore(allowed, allowed));
      }
      var semaphore = semaphores[directoryPath];
      ThreadPool.QueueUserWorkItem(
        (state) =>
        {
          semaphore.WaitOne();
          try
          {
            DoFtpOperation(filePathCapture);
          }
          finally
          {
            semaphore.Release();
          }
        }, null);
    }
  }
}
公共类示例
{
public void processAllFileAsync()
{
var信号量=新字典();
foreach(GetFiles()中的字符串文件路径)
{
string filePathCapture=filePath;//需要正确执行闭包。
字符串directoryPath=Path.GetDirectoryName(filePath);
if(!semaphores.ContainsKey(directoryPath))
{
int allowed=并发操作的数量;
Add(directoryPath,new信号量(allowed,allowed));
}
var信号量=信号量[directoryPath];
ThreadPool.QueueUserWorkItem(
(州)=>
{
WaitOne()信号量;
尝试
{
doftpopulation(filePathCapture);
}
最后
{
semaphore.Release();
}
},空);
}
}
}

线程粒度是多少?每个上传的文件一个线程,或者每个目录一个线程?你的框架目标是什么?您是否正在使用
4.0
?@Unsliced:N每个目录的线程。对于每个目录,我必须同时传输更多的文件。线程粒度是多少?每个上传的文件一个线程,或者每个目录一个线程?你的框架目标是什么?您是否正在使用
4.0
?@Unsliced:N每个目录的线程。对于每个目录,我必须同时传输更多文件。不,我需要为每个目录设置一个计数器,以计算我当前在每个目录中移动的文件数。不,我需要为每个目录设置一个计数器,以计算我当前在每个目录中移动的文件数。对不起,我忘了说我正在使用3.5。信号量lim是一个4特性。实际上它看起来是一个很好的特性。@Dierre:这是一个lambda表达式
state
是委托的一个参数(由lamda表达式形成),我完全忽略了它。另外请注意,此解决方案可以阻止(通过
WaitOne
)我通常建议不要使用的
ThreadPool
线程,但在您的情况下,它不会损害任何东西。很抱歉,我忘了说我正在使用3.5。信号量lim是一个4特性。实际上它看起来是一个很好的特性。@Dierre:这是一个lambda表达式
state
是委托的一个参数(由lamda表达式形成),我完全忽略了它。还请注意,此解决方案可以阻止(通过
WaitOne
)我通常建议不要使用的
ThreadPool
线程,但在您的情况下,它不会损害任何东西。