S
samiarja
Guest
Hi C# programmer all around the world,
I am working on an application that uses SFTP to transfer data from a remote server to a local server over the network, and then the data are converted from .db to .csv format.
In a real world scenario, we are using a hardware for data acquisition, each of these hardware is mounted on a mining vehicle. The data transfer happen only when the hardware enter the network zone, and in real life situation few vehicle will enter the network zone and then leave, in this case the data should be transferred during their presence within the network and once they leave the data transfer stream stop until they come again. Usually there is 20 vehicle. Each hardware has a specific IP address, the C# program read these IP from a JSON file and then ping each IP to see which hardware is within the network.
My code is single threaded, that's mean if two vehicle was within the network, the data transfer perform only for one vehicle at a time, and sometime the other vehicle leave the network as soon as the first data transfer finish, which make the application unreliable and slow.
Below is a snapshot of my single threaded code
filePath = @"C:\temp\JSON\app-db.json";
string text = File.ReadAllText(filePath);
var currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);
string host = item.IPaddress;
string username = item.username;
string password = item.password;
string remoteDirectory = item.sourcefolder;
string localDirectory = item.destfolder;
string filextension = item.filextension;
string removedownloaded = item.removedownloaded.ToString();
using (SftpClient sftp = new SftpClient(host, username, password))
{
try
{
sftp.Connect();
var files = sftp.ListDirectory(remoteDirectory);
foreach (var file in files)
{
try
{
string remoteFileName = file.Name;
if ((file.Name.EndsWith(filextension)) || (file.Name.EndsWith(filextension.ToLower())) || (file.Name.EndsWith(filextension.ToUpper())))
{
using (Stream file1 = File.OpenWrite(Path.Combine(localDirectory, remoteFileName)))
{
string path = remoteDirectory + "/" + remoteFileName;
sftp.DownloadFile(path, file1);
if (removedownloaded == "1")
{
sftp.Delete(path);
}
else
{
sftp.DownloadFile(path, file1);
}
}
}
}
catch (Exception er1)
{
//MessageBox.Show("An exception has been caught " + er1.ToString());
}
}
}
catch (Exception entry)
{
MessageBox.Show(entry.Message);
}
//finally
//{
// sftp.Disconnect();
//}
}
}
How to convert this code to multi-thread? Thanks in advance for your time and suggestion.
Sami Arja
Continue reading...
I am working on an application that uses SFTP to transfer data from a remote server to a local server over the network, and then the data are converted from .db to .csv format.
In a real world scenario, we are using a hardware for data acquisition, each of these hardware is mounted on a mining vehicle. The data transfer happen only when the hardware enter the network zone, and in real life situation few vehicle will enter the network zone and then leave, in this case the data should be transferred during their presence within the network and once they leave the data transfer stream stop until they come again. Usually there is 20 vehicle. Each hardware has a specific IP address, the C# program read these IP from a JSON file and then ping each IP to see which hardware is within the network.
My code is single threaded, that's mean if two vehicle was within the network, the data transfer perform only for one vehicle at a time, and sometime the other vehicle leave the network as soon as the first data transfer finish, which make the application unreliable and slow.
Below is a snapshot of my single threaded code
filePath = @"C:\temp\JSON\app-db.json";
string text = File.ReadAllText(filePath);
var currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);
string host = item.IPaddress;
string username = item.username;
string password = item.password;
string remoteDirectory = item.sourcefolder;
string localDirectory = item.destfolder;
string filextension = item.filextension;
string removedownloaded = item.removedownloaded.ToString();
using (SftpClient sftp = new SftpClient(host, username, password))
{
try
{
sftp.Connect();
var files = sftp.ListDirectory(remoteDirectory);
foreach (var file in files)
{
try
{
string remoteFileName = file.Name;
if ((file.Name.EndsWith(filextension)) || (file.Name.EndsWith(filextension.ToLower())) || (file.Name.EndsWith(filextension.ToUpper())))
{
using (Stream file1 = File.OpenWrite(Path.Combine(localDirectory, remoteFileName)))
{
string path = remoteDirectory + "/" + remoteFileName;
sftp.DownloadFile(path, file1);
if (removedownloaded == "1")
{
sftp.Delete(path);
}
else
{
sftp.DownloadFile(path, file1);
}
}
}
}
catch (Exception er1)
{
//MessageBox.Show("An exception has been caught " + er1.ToString());
}
}
}
catch (Exception entry)
{
MessageBox.Show(entry.Message);
}
//finally
//{
// sftp.Disconnect();
//}
}
}
How to convert this code to multi-thread? Thanks in advance for your time and suggestion.
Sami Arja
Continue reading...