如何在将委托功能用作新线程时更新GUI?我正在尝试更新一个标签,它会告诉用户应用程序的进度状态,但这样一来,它只会在完成后更新,而不是在完成时更新。
public partial class Form1 : Form
{
private void button1_Click_1(object sender, EventArgs e)
{
new Thread(new ThreadStart(delegate()
{
int currentFile= 0;
int allFiles = Source.FilePaths.Count;
foreach (var path in Source.FilePaths)
{
var file = new File(path, Source.DestDirPath, System.IO.File.ReadAllLines(path));
file.saveSQL();
currentFile++;
int rounded = (int)((currentFile/ allFiles) * 100);
CompletionLabel.Invoke(new MethodInvoker(() => Completion.Text = "Completion: " + rounded + "%"));
}
})).Start();
}
}
我想尽可能简单,因为它只是一个简单的转换器,它将一些文本文件转换为sql文件。
答案 0 :(得分:0)
不,你的问题是整数除法。要修复它,您只需要转换为double
或float
。如果您注意到我的第一条评论,您就会得到答案。
int rounded = (int)(((double)currentFile/ allFiles) * 100);
分割两个整数时,结果始终为整数。对于 例如,7/3的结果是2.确定剩下的7 / 3,使用余数运算符(%)。获得商作为理性 数字或分数,给予被除数或除数类型float或type 双。如果表达被除数,您可以隐式指定类型 通过在数字的右侧放置一个数字或将除数作为小数 小数点,如下例所示