【发布时间】:2014-12-29 12:52:50
【问题描述】:
我在 .NET 3.5 中有程序(winform),它可以将文件发送到 ftp 服务器。我想要进度条,我尝试在后台使用线程,但我有错误。
这是我的代码示例:
private void Odeslat_Click(object sender, EventArgs e)
{
Thread thread = new Thread(Process);
thread.IsBackground = true;
thread.Start();
}
public void Process()
{
button1.Enabled = false;
button2.Enabled = false;
foreach (string Prodejna in SeznamProdejen)
{
i = i + 1;
Update1(i);
//some long task
}
}
public void Update1(int i)
{
if (InvokeRequired)
{
this.BeginInvoke(new Action<int>(Update1), new object[] { i });
return;
}
progressBar1.Value = i;
}
这里有错误:
button1.Enabled = false;
这个错误:
{"Operation between threads is not valid: Access to the control button1 took place from another thread than the thread under which it was created."}
在公共无效进程中,我有一个程序,它将文件发送到 ftp 服务器。
!!!!!!但是现在我有问题,这是我的代码示例:!!!!!!
private void Odeslat_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = false;
Thread thread = new Thread(Process);
thread.IsBackground = true;
thread.Start();
MessageBox.Show("Výsledek odesílání naleznete v souboru vysledek.txt", "Výsledek");
button1.Enabled = true;
button2.Enabled = true;
}
public void Process()
{
foreach (string Prodejna in SeznamProdejen)
{
i = i + 1;
Update1(i);
//some long task
..............
Stream reqStream = request.GetRequestStream(); //PROBLEM
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
.................
}
}
public void Update1(int i)
{
if (InvokeRequired)
{
this.BeginInvoke(new Action<int>(Update1), new object[] { i });
return;
}
progressBar1.Value = i;
}
问题:程序何时靠近 Stream reqStream = request.GetRequestStream();跳转到私有 void Odeslat_Click 到 messagebox.show。在那之后……我不明白为什么?
【问题讨论】:
-
这个 wpf 是什么样的项目?赢形式?其他的?
-
试试
Dispatcher.Invoke(new MethodInvoker(delegate{button1.Enabled = false;}));而不是button1.Enabled = false; -
我的程序必须在 .NET Framework 3.5 中...这个 .Net 没有 Dispatcher :/
-
您是否希望在线程结束时重新启用按钮?
标签: c# multithreading progress-bar