I am using WinSCP .NET assembly for uploading files. I want to upload multiple files asynchronous way. I have created a method but it works as single upload.
public class UploadingData {
private SessionOptions _sessionOptions;
private Session _session;
//connection etc
private void OnConnected() {
foreach (var fileInfo in localFilesList)
{
var task = Task.Factory.StartNew(() => UploadFilesAsync(fileInfo));
}
}
private async Task UploadFilesAsync(string file) {
string remoteFilePath = _session.TranslateLocalPathToRemote(file, @"/", "/test_data_upload");
var uploading = _session.PutFiles(file, remoteFilePath, false);
//When Done
await Task.Run(() => Thread.Sleep(1000));
}
}
Please suggest me correct way. Thanks
The API of the Session
class can be used from a single thread only. If you use it from multiple threads, it will block.
So if you need parallel transfers, you have to create a separate Session
instance for each thread.
private async Task UploadFilesAsync(string file)
{
using (Session session = new Session())
{
session.Open(_sessionOptions);
string remoteFilePath =
RemotePath.TranslateLocalPathToRemote(file, @"/", "/test_data_upload");
session.PutFiles(file, remoteFilePath, false).Check();
}
...
}
See also WinSCP article Automating transfers in parallel connections over SFTP/FTP protocol with code examples for C# and PowerShell.
Note that you should not use more than few sessions/threads (about 4). With more sessions, you will hardly get better throughput.
You can use BackgroundWorker class like this:
string[] images = new string[50];
foreach (var path in images)
{
BackgroundWorker w = new BackgroundWorker();
//50 independently async Threads
w.DoWork += delegate (object s, DoWorkEventArgs args)
{
Upload(args.Argument.ToString());
};
w.RunWorkerAsync(path);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.