我有以下的多线程FTP进程。 MaxThread是4,因为我们的目标客户端已将我们限制为4个线程。
ThreadPool.SetMaxThreads(workerThreads, completionPortThreads);
foreach (var loan in fileList)
{
// Create an MRE for each thread.
var handle = new ManualResetEvent(false);
// Store it for use below.
handles.Add(handle);
ThreadPool.QueueUserWorkItem(FtpProcess, Tuple.Create(loan, handle));
}
// Wait for threads to finish.
WaitHandle.WaitAll(handles.ToArray());
在FtpProcess方法内部创建连接,QueueUserWorkItem等待打开的线程继续。但是连接不会发生。我在此收到会话超时错误。
private static void FtpProcess(object loanObject)
{
var tuple = (Tuple<KeyValuePair<string, List<string>>, ManualResetEvent>)loanObject;
var loan = tuple.Item1;
var client = new SftpClient(removed for security but this is filled in);
if (!client.IsConnected)
{
client.Connect();
}
现在,如果我不使用QueueUserWorkItem并且只使用新线程(FtpProcess).Start那么它可以工作并多次连接,直到发生上限问题。
我是否遗漏了某些内容或完全误解了此.NET流程的整个底层代码?