c# – 使用库中的函数同步wpf进度条的正确方法

前端之家收集整理的这篇文章主要介绍了c# – 使用库中的函数同步wpf进度条的正确方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用库中的函数来处理非常大的单词文件,我无法更改此功能.
在处理时,我想显示一个进度条,因为无论哪种方式,应用程序看起来都冻结了,用户也不知道它实际上正在工作.
现在我正在使用这样的工人:
private void btnClick(object sender,RoutedEventArgs e)
{ 
    BackgroundWorker worker = new BackgroundWorker();
    worker.RunWorkerCompleted += worker_RunWorkerCompleted;
    worker.WorkerReportsProgress = true;
    worker.DoWork += worker_DoConvertOne;
    worker.ProgressChanged += worker_ProgressChanged;
    worker.RunWorkerAsync();
}

private void worker_DoConvertOne(object sender,DoWorkEventArgs e)
{
    var worker = sender as BackgroundWorker;
        //The progress bar is filled on 20%
        worker.ReportProgress(0);
        worker.ReportProgress(10);
        worker.ReportProgress(20);

        //Processing
        myLongLastingFunction(bigWordFile);

        //The progress bas is full
        worker.ReportProgress(100,"Done Processing.");
}

private void worker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
    MessageBox.Show("Converting finished!");
    TestProgressBar.Value = 0;
    ProgressTextBlock.Text = "";
}

private void worker_ProgressChanged(object sender,ProgressChangedEventArgs e)
{
    TestProgressBar.Value = e.ProgressPercentage;
    ProgressTextBlock.Text = (string)e.UserState;
}

它正在做这项工作,但它是一种解决方法,我想知道是否有一种正确的方法解决我的问题.提前致谢.

猜你在找的C#相关文章