title: SSH Connect Using Parallel Threading C#
body: <p>What is the error Please?
  the task never completed</p>
  
  <p><code>using Renci.SshNet;</code>
  in <code>Program.cs</code> </p>
  
  <pre><code>static List&lt;string&gt; tasks = new List&lt;string&gt;();
  static List&lt;Server&gt; servers = new List&lt;Server&gt;();
  </code></pre>
  
  <p>and in the <code>Main</code></p>
  
  <pre><code>var serverFile = File.ReadAllLines("E:/servers.txt");
  foreach (var line in serverFile)
  {
      var arrayOfInfo = line.Split('|');
      servers.Add(new Server() { IP = arrayOfInfo[0], Username = arrayOfInfo[1], Password = arrayOfInfo[2], IsBusy = false });
  }
  if (servers.Count == 0)
      Console.WriteLine("No SSH Selected");
  
  var rangesFile = File.ReadAllLines("E:/ranges.txt");
  for (int i = 0; i &lt; rangesFile.Count(); i++)
  {
      tasks.Add(string.Format("./svmap.py {0} &gt;&gt; output.txt", rangesFile));
  }
  if (servers.Count == 0)
      Console.WriteLine("No Ranges Selected");
  </code></pre>
  
  <p>and the code to connect by parallel</p>
  
  <pre><code>Parallel.ForEach(tasks, async (task) =&gt;
  {
      while (servers.Where(m =&gt; m.IsBusy == false).Count() == 0)
      {
          Thread.Sleep(1000);
      }
  
  
      List&lt;Server&gt; notBusy = servers.Where(m =&gt; m.IsBusy == false).ToList();
      var selectedServer = notBusy.FirstOrDefault();
  
      try
      {
          selectedServer.IsBusy = true;
          Console.WriteLine("Order {0} is given", task, selectedServer.IP);
          await RunTask(selectedServer, task);
          Console.WriteLine("{0} Completed", task);
          selectedServer.IsBusy = false;
      }
      catch (Exception e)
      {
          Console.WriteLine(e.Message);
      }
  });
  </code></pre>
  
  <p>and we have a static method</p>
  
  <pre><code>static public async Task RunTask(Server server, string task)
  {
      using (var client = new SshClient(server.IP, server.Username, server.Password))
      {
          client.Connect();
          client.RunCommand(task);
          client.Disconnect();
      }
  }
  </code></pre>
  
  <p>in <code>Server.cs</code></p>
  
  <pre><code>public string IP { get; set; }
  public string Username { get; set; }
  public string Password { get; set; }
  public bool IsBusy { get; set; }
  </code></pre>
  
  <p>but the program never completes it's job, could anyone help me fix the bugs ?
  i've tried for 72h ..</p>
  
  <p>the program gives the orders but i don't know what is happening next .</p>