ftp上传文件 用进度条progressBar显示进度(在线等)

villain2008 2009-02-03 01:31:18

public class upload : Mypublic
{
public delegate void delProgress(int value);
public event delProgress eventProgress;

public delegate void delFinish();
public event delFinish eventFinish;

public string fileName = "";
private long current = 0;
private long total = 1;
private int percent = 0;
int temp = 0;

public void Upload()
{
FileInfo fileinfo = new FileInfo(fileName);
string uri = "ftp://" + IpAddress + "/" + fileinfo.Name;
FtpWebRequest reqFtp;

reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + IpAddress + "/" + fileinfo.Name));
reqFtp.Credentials = new NetworkCredential(Userid, Password);
reqFtp.KeepAlive = false;
reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
reqFtp.UseBinary = true;

reqFtp.ContentLength = fileinfo.Length;
total = fileinfo.Length;

int bufferlenth = 2048;
byte[] buff = new byte[bufferlenth];
int contentLen;

FileStream fs = fileinfo.OpenRead();

try
{
Stream strm = reqFtp.GetRequestStream();
contentLen = fs.Read(buff, 0, bufferlenth);

while (contentLen != 0)
{
strm.Write(buff, 0, bufferlenth);
contentLen = fs.Read(buff, 0, bufferlenth);

current += contentLen;
temp = Int32.Parse((100 * current / total).ToString());
if (temp != percent)
{
eventProgress(temp);
percent = temp;
}
}
strm.Close();
fs.Close();

eventFinish();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
}
}

按钮事件
private void button1_Click(object sender, EventArgs e)
{
if (DialogResult.OK == this.openFileDialog1.ShowDialog())
{
upload up = new upload();
up.IpAddress = "192.168.1.31";
up.Userid = "Administrator";
up.Password = "110";
up.fileName = this.openFileDialog1.FileName;

up.eventProgress += new upload.delProgress(up_eventProgress);
up.eventFinish += new upload.delFinish(up_eventFinish);

Thread t = new Thread(new ThreadStart(up.Upload));
t.Start();
}
}

void up_eventFinish()
{
up_eventProgress(100);
MessageBox.Show("上传成功!");
}

void up_eventProgress(int value)
{
this.progressBar1.Value = value;
this.label1.Text = value.ToString() + "%";
}

运行之后报错 :线程间操作无效:从不是创建控件ProgressBar1的线程访问它
请教高手!在线等~~
...全文
927 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Enricbkyi 2010-08-26
  • 打赏
  • 举报
回复
强烈学习中!
villain2008 2009-02-03
  • 打赏
  • 举报
回复
感谢 stringkakaa !!
问题解决了!
同时也感谢大家,谢谢!
顾君彦 2009-02-03
  • 打赏
  • 举报
回复
把窗口对象传到这个类里 public class upload : Mypublic

例如,窗口对象名为 up
则在更改进度时,调用方法为:
将原来 eventProgress(temp);
改为
up.invoke(up.eventProgress,new object[]{temp});

试试。
stringkakaa 2009-02-03
  • 打赏
  • 举报
回复
你把你的up_eventProgress方法写成这样试试看。

void up_eventProgress(int value)
{
if(this.InvokeRequired)
{
this.Invoke(new delProgress(up_eventProgress),value)
}
else
{
this.progressBar1.Value = value;
this.label1.Text = value.ToString() + "%";
}
}
顾君彦 2009-02-03
  • 打赏
  • 举报
回复
动用到窗体上显示属于的界面绘制和重绘工作的一部分。

这部分工作必须使用窗体本身的消息循环线程来进行处理,其它工作线程不会起到立即更新的作用。

可以使用 invoke方法,将窗体中修改进度条属性的方法单独写个方法。

在工作线程中,调用窗体对象的invoke方法去返调窗体中的修改进度条方法的方法实例(也可以使用委托传递)


这样就可以保证工作程中执行的那个修改另一个窗体进度条的操作是使用了窗体身体消息循环线程进行执行的。这样就可以保证即时的更新UI.
villain2008 2009-02-03
  • 打赏
  • 举报
回复
current += contentLen;
temp = Int32.Parse((100 * current / total).ToString());
if (temp != percent)
{
eventProgress(temp); 这里应该是percent
percent = temp;
}

然后运行之后报的错是 :线程间操作无效:从不是创建控件label1的线程访问它

请各位高手帮我改改 究竟是哪里出错了

villain2008 2009-02-03
  • 打赏
  • 举报
回复
楼上的能否给具体点的
symbol441 2009-02-03
  • 打赏
  • 举报
回复
http://www.cnblogs.com/symbol441/archive/2007/11/15/960529.html
yagebu1983 2009-02-03
  • 打赏
  • 举报
回复
用委托试试。。。
因为线程不能直接访问控件。。。
king19840811 2009-02-03
  • 打赏
  • 举报
回复
Invoke或beginInvoke

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧