我的代码将文件从远程服务器下载到我们的服务器。
通常,一个文件最多需要一分钟左右的时间才能完成下载。但有时下载时间会超过 5 分钟。
如果当前下载时间超过 5 分钟,是否可以重新开始下载?
Tamir.SharpSsh.SshTransferProtocolBase sshCp;
sshCp = new Scp(sessionOptions.HostName, sessionOptions.UserName);
sshCp.Password = sessionOptions.Password;
sshCp.Connect();
foreach (UmtsFilesStruct u in Array)
{
try
{
sshCp.Get(u.RemoteFilePath, u.LocalFilePath);
}
catch (Exception ex)
{
using (StreamWriter w = File.AppendText(Logger._LogName))
{
string error = string.Format("error downloading file [{0}], Remote: {1} & Local: {2} ",
ex.Message, u.RemoteFilePath, u.LocalFilePath);
Logger.Log(error, w);
}
}
}
最佳答案
您可以尝试使用 System.Diagnostics.Stopwatch
作为计时器。当 timer.Elapsed >= TimeSpan.FromMinutes(5)
关闭您的连接并再次运行您发布的代码。如果某个实例的文件下载时间少于 1 分钟,您一定不要忘记停止计时器并将其值重置为零,以防止在实际下载文件时再次下载。
附言如果您在使用 System.Diagnostics.Stopwatch
System.Timers.Timer
关于c# - SharpSSh : Restart file transfer if current file transfer too slow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23699796/