BackgroundWorker WPF difficulties with Progress Bar

后端 未结 2 538
失恋的感觉
失恋的感觉 2020-12-20 07:41

I am developing a WPF, C# application and I have to read a very big CSV file and write to a database. This process takes a long time so I want to at least show a progress ba

相关标签:
2条回答
  • 2020-12-20 08:02

    is the varible i int? if it is, the value of (100 * i) / 10000 will return 0 while i < 100. and the param of ReportProgress method should be a precent value. you can change it to worker.ReportProgress(i);,try it.

    0 讨论(0)
  • 2020-12-20 08:09

    This will not compile as-is, but should get you started in the right direction:

    private readonly BackgroundWorker worker
        = new BackgroundWorker { WorkerReportsProgress = true };
    
    public MainWindow()
    {
        InitializeComponent();
    
        worker.DoWork += worker_DoWork;
        worker.ProgressChanged += worker_ProgressChanged;
    }
    
    private void worker_DoWork(object sender, DoWorkEventArgs doWorkEventArgs)
    {
        // Do some long process, break it up into a loop so you can periodically
        //  call worker.ReportProgress()
    
        worker.ReportProgress(i);  // Pass back some meaningful value
    }
    
    private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        prgBar.Value = Math.Min(e.ProgressPercentage, 100);
    }
    
    0 讨论(0)
提交回复
热议问题