我创建了一个Windows窗体程序,用于分解文件并将其组件发送到服务器。这些文件很大,所以我创建了一个progressBar,因此用户不会在事务发生时冻结它。我想要做的是有一些机制,只有当所有线程完成而不会阻塞UI线程时才会主动触发(再次,因此平台不会认为它被冻结)。我能想到的最好的方式是被动地“等到真实”,但我觉得必须有更好的方式来做到这一点。我尝试过尝试创建一个事件或回调,但老实说,我刚刚结束时比我开始时更困惑。下面是我现在怎么做这样一个例子:如何在进度条完成时触发事件
public partial class Program : Form
{
private readonly OpenFileDialog _ofd = new OpenFileDialog();
public delegate void BarDelegate();
private string _path;
private void button1_Click(object sender, EventArgs e)
{
if (_ofd.ShowDialog() != DialogResult.OK) return;
textBox1.Text = _ofd.SafeFileName;
_path = _ofd.FileName;
}
private void button2_Click(object sender, EventArgs e)
{
var allLinesFromFile = File.ReadAllLines(_path);
progressBar1.Minimum = 0;
progressBar1.Maximum = allLinesFromFile.Length;
Task.Factory.StartNew(() => Parallel.ForEach(allLinesFromFile, DoSomething));
while (progressBar1.Value < progressBar1.Maximum) //there has to be a better way to do this...
{
MessageBox.Show("Please wait.", "Record Poster", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
//some processes here which should only take place after all threads are complete.
var postingComplete = MessageBox.Show("The posting is complete!", "Record Poster", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
if (postingComplete == DialogResult.OK) Environment.Exit(0);
}
private void DoSomething(string record)
{
//some string manipulation and server transactions here
BeginInvoke(new BarDelegate(() => progressBar1.Increment(1)));
}
}
你是为进度条提供价值的人 - 所以当你达到100时,请调用你想做的方法。首先,你不需要TaskFactory。 –
第三 - 有多少条线?看起来像显示一个消息框会比处理本身花费更多的时间 –
您可以编写'ValueChanged'事件并检查'Value == Maximum' – TaW