在新窗口中显示进度条

时间:2013-03-31 08:31:27

标签: c# wpf progress-bar

如何在MainWindow前面的新窗口中创建进度条?现在,我的prgoressbar将被展示。但是,我只使用.Show()函数,窗口将不显示任何内容;当我使用.ShowDialog()函数时,窗口会显示进度条但不起作用。我知道Show和ShowDialog之间的区别在于Show会立即返回。我的问题是如何让进度条窗口工作?

1 个答案:

答案 0 :(得分:0)

我假设同样的逻辑适用于WPF

解决方案基于WinForm

Form1中

private void button1_Click(object sender, EventArgs e)
{
    Form2 form = new Form2();
    // i have used thread pool just to simulate virtual progress from 1 to 100
    ThreadPool.QueueUserWorkItem(ShowProgressHandler, form);

    form.ShowDialog();

}

private void ShowProgressHandler(object o)
{
    Form2 form = o as Form2;
    for (int i = 0; i < 100; i++)
    {
        int i1 = i;
        this.Invoke((MethodInvoker) (() => form.OnProgress(i1)));
        Thread.Sleep(100);
    }
}

窗体2

public event Action<int> Progress;

protected internal virtual void OnProgress(int obj)
{
    progressBar1.Value = obj;
}