I have a C# code in which I am executing on Visual Studio 2017. My aim is to start multiple threads in parallel and connect to different IPs via SSH, execute a command on these devices and return the output to the Console. An example of connecting to one such device with IP: 192.168.46.126 is as follows:
Thread myNewThread = new Thread(() => sendCommand("192.168.46.126", command));
myNewThread.Start();
The sendCommand function is as follows:
void sendCommand(string IPaddr, string command)
{
using (var sshClient = new SshClient(IPaddr, username, password))
{
sshClient.Connect();
sshClient.Runcommand(command); //Takes a lot of time to execute
}
}
The sshClient.runCommand(command) takes a lot of time to execute. I need to run all these threads in parallel to connect to each device and print their statuses to the Console. In case a thread takes too long, I need to have a timeout option. I searched online but their seems to be no timeout option on the RunCommand function. The only timeout option seems to be on the connect() function implemented via following code:
client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(10);
Is their a way I can implemet a timout for the runCommand line of code?