正如你在这里看到的,有一个用于ftp传输的后台工作者。
Franks 答案显示了具有一些覆盖方法的新类,因此可以获得传输速度及其百分比。
我想取消运行上传的后台工作者,但我不知道如何。
实现
public UpAndDownloadForm(Job pJob)
{
InitializeComponent();
this.job = pJob;
bGroundWorker = new BackgroundWorker();
bGroundWorker.WorkerReportsProgress = true;
bGroundWorker.WorkerSupportsCancellation = true;
bGroundWorker.DoWork += new DoWorkEventHandler(bGroundWorker_DoWork);
bGroundWorker.ProgressChanged += new ProgressChangedEventHandler(bGroundWorker_ProgressChanged);
bGroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bGroundWorker_RunWorkerCompleted);
bGroundWorker.RunWorkerAsync(pJob);
}
做工:
void bGroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
streamWithTransferSpeedWorker = (BackgroundWorker)sender;
Job job = (Job)e.Argument;
string[] files = Directory.GetFiles(job.GetSourceFolder(), "*.*", SearchOption.AllDirectories);
if (bGroundWorker.CancellationPending) //Falls der bGWorker abgebrochen wurde, beendet man diesen wohl einfach mit e.Cancel;
{
e.Cancel = true;
return;
}
foreach (string file in files)
{
char[] temp = file.ToCharArray(); //Dateipfad in CharArray speichern
Array.Reverse(temp); //Reihenfolge ändern (von hinten nach vorne)
string fileReversed = new string(temp); //als string speichern
int index = fileReversed.IndexOf(@""); //die Zeichenlänge des DateiNAMENs auslesen
string fileOnlyName = file.Substring(file.Length - index, index); //nur den Dateinamen auslesen
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(job.GetDestinationFolder() + "\" + fileOnlyName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(Manager._user, Manager._password);
var response = (FtpWebResponse)request.GetResponse(); //WebResponse response = request.GetResponse();
using (Stream source = File.OpenRead(file))
{
using (var destination = new StreamWithTransferSpeed(request.GetRequestStream(), streamWithTransferSpeedWorker, source.Length))//request.ContentLength))
{
source.CopyTo(destination);
}
}
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
}
取消后台工作人员:
private void btnCancelLoad_Click(object sender, EventArgs e)
{
if (bGroundWorker.IsBusy)
{
bGroundWorker.CancelAsync();
}
if (streamWithTransferSpeedWorker.IsBusy)
{
streamWithTransferSpeedWorker.CancelAsync();
}
}
如您所见,我已经尝试了一些东西,但这没有任何意义。
用于启动上载的后台工作者 - CancellationPending
-标志在按钮单击事件之后true
,但上载仍在运行。
我错过了什么吗?
在写这个问题时,我想过取消CopyTo
方法,但我什至不知道这是否有效,或者这是否是问题/解决方案。
感谢您的帮助。
编辑1:
在这里,您可以看到转发的后台工作者
using (var destination = new StreamWithTransferSpeed(request.GetRequestStream(), streamWithTransferSpeedWorker, source.Length))
。
编辑2:
正如T MCKeown所说:布尔
值的实例上传表格.cs:
private void btnCancelLoad_Click(object sender, EventArgs e)
{
if (bGroundWorker.IsBusy)
{
bGroundWorker.CancelAsync();
Manager._cancelBackgroundWorker = true;
}
}
另外还有我的部分:
StreamWithTransferSpeed.cs:
if (Manager._cancelBackgroundWorker)
{
this.worker.CancelAsync(); //this is useless, because the Backgroundworker is still running
Manager._cancelBackgroundWorker = false;
innerStream.Close(); //this is dirty, but it works, after the Closing the backgroundworker is canceled
}
在这里你可以看到CancelAsync()
.它在 Console.WriteLine("bGW direkt unter der exception abgebrochen");
执行:
上传表格.cs:
void bGroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = (BackgroundWorker)sender;
Job job = (Job)e.Argument;
string[] files = Directory.GetFiles(job.GetSourceFolder(), "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
if (bGroundWorker.CancellationPending) //Falls der bGWorker abgebrochen wurde, beendet man diesen wohl einfach mit e.Cancel;
{
e.Cancel = true; //Sinnlos da es eh nicht funktioniert
Console.WriteLine("bGW direkt unter foreach abgebrochen");
return;
}
char[] temp = file.ToCharArray(); //Dateipfad in CharArray speichern
Array.Reverse(temp); //Reihenfolge ändern (von hinten nach vorne)
string fileReversed = new string(temp); //als string speichern
int index = fileReversed.IndexOf(@""); //die Zeichenlänge des DateiNAMENs auslesen
string fileOnlyName = file.Substring(file.Length - index, index); //nur den Dateinamen auslesen
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(job.GetDestinationFolder() + "\" + fileOnlyName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(Manager._user, Manager._password);
var response = (FtpWebResponse)request.GetResponse(); //WebResponse response = request.GetResponse();
try
{
using (Stream source = File.OpenRead(file))
{
using (var destination = new StreamWithTransferSpeed(request.GetRequestStream(), worker, source.Length))//request.ContentLength))
{
source.CopyTo(destination);
}
}
}
catch (Exception ex)
{
if (ex.GetType() == typeof(ObjectDisposedException))
{
Console.WriteLine("Upload canceled.");
if (bGroundWorker.CancellationPending) //Falls der bGWorker abgebrochen wurde, beendet man diesen wohl einfach mit e.Cancel;
{
e.Cancel = true; //Sinnlos da es eh nicht funktioniert
Console.WriteLine("bGW direkt unter der exception abgebrochen");
return;
}
}
}
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
}
创建类型为 bool
的实例_cancel
变量
private void btnCancelLoad_Click(object sender, EventArgs e)
{
_cancel = true;
...
}
然后在这里检查:
foreach (string file in files)
{
if (_cancel ) return;
char[] temp = file.ToCharArray(); //Dateipfad in CharArray speichern
...
将 if 语句添加到您可能要检查取消的任何其他位置。
对编辑2的回应:
您将代码更改为以下内容:
foreach (string file in files)
{
if (bGroundWorker.CancellationPending) //Falls der bGWorker abgebrochen wurde, beendet man diesen wohl einfach mit e.Cancel;
{
e.Cancel = true; //Sinnlos da es eh nicht funktioniert
Console.WriteLine("bGW direkt unter foreach abgebrochen");
return;
}
我建议你做这样的事情:
foreach (string file in files)
{
if (Manager._cancelBackgroundWorker) //Falls der bGWorker abgebrochen wurde, beendet man diesen wohl einfach mit e.Cancel;
{
e.Cancel = true; //Sinnlos da es eh nicht funktioniert
Console.WriteLine("bGW direkt unter foreach abgebrochen");
return;
}
单击"取消"时将此布尔值设置为 true
是吗?
将Cancel
检查代码放入 Foreach 循环中
if (bGroundWorker.CancellationPending) //Falls der bGWorker abgebrochen wurde, beendet man diesen wohl einfach mit e.Cancel;
{
e.Cancel = true;
return;
}