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的线程访问它
请教高手!在线等~~
解决方案 »
- treeView显示我电脑里的资源列表,为什么设备未就绪
- image 控件显示xml 文件
- 把数据库里的图片值输出到图片框,请问如何操作?
- 如何实现在文本输入框中过滤掉非法的关键字(如:你妈,王八蛋等),请给出例题代码!
- [急]VS2005"更新数据智能任务"出错
- 求 王凯明 浅析C#中的套接字编程 原代码
- 有熟悉EXCEl的QueryTable的吗?如何用OleDb模式访问MS SQLServer ???
- 一个关于C#特性的问题,希望指教!
- C# 的设置特性的作用,列举举例 即给分!
- 求助,Webbroswer控件模拟点击后网页源码不更新!!!!
- 运算符“|” 在这里是什么意思?
- c#l连接sql失败,请教
temp = Int32.Parse((100 * current / total).ToString());
if (temp != percent)
{
eventProgress(temp); 这里应该是percent
percent = temp;
} 然后运行之后报的错是 :线程间操作无效:从不是创建控件label1的线程访问它 请各位高手帮我改改 究竟是哪里出错了
。这样就可以保证工作程中执行的那个修改另一个窗体进度条的操作是使用了窗体身体消息循环线程进行执行的。这样就可以保证即时的更新UI.
{
if(this.InvokeRequired)
{
this.Invoke(new delProgress(up_eventProgress),value)
}
else
{
this.progressBar1.Value = value;
this.label1.Text = value.ToString() + "%";
}
}
则在更改进度时,调用方法为:
将原来 eventProgress(temp);
改为
up.invoke(up.eventProgress,new object[]{temp});试试。
问题解决了!
同时也感谢大家,谢谢!