在form1设计器中我有一个backgroundworker,我在设计器中将WorkerReportsProgress和WorkerSupportsCancellation设置为true。
我在form1 designer中有一个progressBar1。
两个backgorundworker事件:DoWork和ProgressChanged。 在DoWork事件中,我调用的方法是从另一个类上传ftp文件:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
uploadftp.upload(@"c:\temp\FtpTestFile.txt", false);
}
在form1构造函数中,我正在启动backgroundworker:
backgroundWorker1.RunWorkerAsync();
这是uploadftp类中的upload方法:
public void upload(string filename, bool uploadtosubortoroot)
{
try
{
FtpWebRequest request = null;
if (uploadtosubortoroot == true)
{
request = (FtpWebRequest)FtpWebRequest.Create(
host + "/" + directory + "/" + Path.GetFileName(filename));
}
else
{
request = (FtpWebRequest)FtpWebRequest.Create(
host + "/" + Path.GetFileName(filename));
}
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(user, pass);
StreamReader sourceStream = new StreamReader(filename);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
catch (Exception err)
{
string t = err.ToString();
}
}
我想要做的是当文件上传时,progressBar将从0%开始进度到100%,其中0%是开始上传的时候,100%是完成上传文件的时候。
在上传方法中,我没有找到任何已注册的已完成事件,因此它会告诉我该文件已上传并已完成。
编辑:
这是我到目前为止所尝试的:
在form1中,我在按钮点击事件中添加了一个按钮:
private void button3_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
在backgorundworker DoWork活动中的form1中我做了:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
uploadftp.upload(@"c:\temp\FtpTestFile.txt", false, (BackgroundWorker)sender);
}
在ProgressChanged事件中,我做了:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
backgorundworker完成的活动仍然是空的。
在上传方法的课程中,我做了:
public void upload(string filename, bool uploadtosubortoroot,BackgroundWorker bworker)
{
try
{
FtpWebRequest request = null;
if (uploadtosubortoroot == true)
{
request = (FtpWebRequest)FtpWebRequest.Create(
host + "/" + directory + "/" + Path.GetFileName(filename));
}
else
{
request = (FtpWebRequest)FtpWebRequest.Create(
host + "/" + Path.GetFileName(filename));
}
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(user, pass);
StreamReader sourceStream = new StreamReader(filename);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
bworker.ReportProgress(0, "Uploading file...");
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
catch (Exception err)
{
string t = err.ToString();
}
}
但它永远不会报告或没有进展。
答案 0 :(得分:1)
将BackgroundWorker
的引用传递给Upload
方法
为BackgroundWorker
:
.ReportProgress
- 执行方法ReportProgress
时更新进度条
MSDN BackgroundWorker.ReportProgress Method (Int32, Object)
.RunWorkerCompleted
- 在执行DoWork
事件的事件处理程序后更新进度条
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
uploadftp.upload(@"c:\temp\FtpTestFile.txt", false, (BackgroundWorker)sender);
}
public void upload(string filename,
bool uploadtosubortoroot,
BackgroundWorker bworker)
{
try
{
//I assume your progressbar.MaxValue = 100
bworker.ReportProgress(0);
FtpWebRequest request = null;
if (uploadtosubortoroot == true)
{
request = (FtpWebRequest)FtpWebRequest.Create(
host + "/" + directory + "/" + Path.GetFileName(filename));
}
else
{
request = (FtpWebRequest)FtpWebRequest.Create(
host + "/" + Path.GetFileName(filename));
}
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(user, pass);
bworker.ReportProgress(20);
StreamReader sourceStream = new StreamReader(filename);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
bworker.ReportProgress(50);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
bworker.ReportProgress(100);
}
catch (Exception err)
{
string t = err.ToString();
}
}