进度条未使用BackgroundWorker进行更新

时间:2015-04-13 09:57:05

标签: c# wpf progress-bar

我试图读取文件并在屏幕上显示进度。 它应显示在进度条(进度)和文本框(progress_text)上。

这是我使用的代码:

public partial class MainWindow : Window
{
    public static List<String> file = new List<String>();
    public long currentPosition = 0;
    String line;
    long length;
    public BackgroundWorker worker = new BackgroundWorker();


    public MainWindow()
    {
        InitializeComponent();

        worker.WorkerReportsProgress = true;
        worker.DoWork += worker_DoWork;
        worker.ProgressChanged += worker_ProgressChanged;
    }

    private void Start(object sender, RoutedEventArgs e)
    {
        worker.RunWorkerAsync();
    }

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker bg = sender as BackgroundWorker;

        Dispatcher.Invoke(() =>
        {
            progress.Minimum = 0;
            progress.Maximum = 100;

            FileInfo fi = new FileInfo(upload.Text);
            length = fi.Length;

            int percent;


            using (StreamReader sr = new StreamReader(upload.Text, System.Text.Encoding.ASCII))
            {
                while (sr.EndOfStream == false)
                {
                    line = sr.ReadLine();
                    file.Add(line);
                    currentPosition += line.Count();

                    percent = (int)(100.0 / length * currentPosition);
                    bg.ReportProgress(percent);
                }
            }
        });
    }

    void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progress.Value = e.ProgressPercentage;
        progress_text.Text = "reading " + currentPosition + " out of " + length;
    }
}

XAML:

<Window x:Class="ProgressBarUploadFile.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    >
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="20*"/>
        <RowDefinition Height="20*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="20*"/>
        <ColumnDefinition Width="20*"/>
    </Grid.ColumnDefinitions>
    <Button Grid.Row="0" Grid.Column="0" Content="Select File" FontSize="15" Click="Upload_file"/>
    <TextBox x:Name="upload" Grid.Row="0" Grid.Column="1" Text="" />
    <Button Grid.Row="1" Grid.Column="0" Content="Start upload" FontSize="15" Click="Start"/>
    <ProgressBar Name="progress" Grid.Row="1" Grid.Column="1" />
    <TextBlock Name="progress_text" Grid.Row="1" Grid.Column="1" VerticalAlignment="Bottom"/>
</Grid>

当我在调试中运行它时,它似乎正在工作。 但只有在完全读取文件时才会更新进度条和文本框。

我遵循了一些教程,例如:

https://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx?f=255&MSPPError=-2147217396

http://www.wpf-tutorial.com/misc-controls/the-progressbar-control/

但我无法弄清楚......

我认为它非常小,但我无法找到它。

谢谢!

2 个答案:

答案 0 :(得分:2)

试试这段代码:

private void Start(object sender, RoutedEventArgs e)
    {
 progress.Minimum = 0;
            progress.Maximum = 100;
        worker.RunWorkerAsync();
    }



BackgroundWorker bg = sender as BackgroundWorker;
            FileInfo fi = new FileInfo(@"File");
            length = fi.Length;

            int percent;


            using (StreamReader sr = new StreamReader(@"File", System.Text.Encoding.ASCII))
            {
                while (sr.EndOfStream == false)
                {
                    line = sr.ReadLine();
                    file.Add(line);
                    currentPosition += line.Count();

                    percent = (int)(currentPosition / length) * 100;
                    bg.ReportProgress(percent);
                    Thread.Sleep(100);
                }
            }

答案 1 :(得分:1)

您的代码存在此问题:

(int)(a / b) * 100会首先计算a/b然后将其转换为int然后再转换为*100,因此在a到达b之前,a/b 1}}始终为0.*(int)(a/b)始终为0,因此最终值0*100将始终为0.

a=b然后(int)(a/b)变为1并且最终值更改为100时,突然间。这就是为什么在完全读取文件之前不会更新进度条

因此,您应该使用percent = (int)(currentPosition * 100 / length);

或者@jmc在评论中建议:使用percent = (int)(100.0 / length * currentPosition);