SSH.NET session timeout on multithreaded

I have below for a multithreaded FTP process. MaxThread is 4 because our target client has limited us to 4 threads.

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());

      

Inside the FtpProcess method, a connection is created and the QueueUserWorkItem waits for the open stream to continue. But the connection won't happen. I am getting a session timeout error in this.

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();
    }

      

Now if I don't use QueueUserWorkItem and just use the new Thread (FtpProcess) .Start, then it works and connects multiple times until there is a cap problem.

Am I missing something or completely misunderstood all the underlying code for this .NET process?

+3


source to share





All Articles