Tôi gặp sự cố như sau:C# Winform ProgressBar và BackgroundWorker
Tôi có Biểu mẫu có tên MainForm. Tôi có một hoạt động dài được thực hiện trên mẫu đơn này.
Trong khi hoạt động dài này đang diễn ra, tôi cần hiển thị một tệp khác từ tên ProgressForm trên đầu trang của MainForm.
ProgressForm chứa thanh tiến trình. Mà cần phải được cập nhật trong khi hoạt động lâu dài đang diễn ra.
Sau khi hoàn thành thao tác dài, ProgressForm sẽ tự động được đóng lại.
Tôi đã viết một số mã như sau:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ClassLibrary
{
public class MyClass
{
public static string LongOperation()
{
Thread.Sleep(new TimeSpan(0,0,30));
return "HelloWorld";
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BackgroungWorker__HelloWorld
{
public partial class ProgressForm : Form
{
public ProgressForm()
{
InitializeComponent();
}
public ProgressBar ProgressBar
{
get { return this.progressBar1; }
set { this.progressBar1 = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ClassLibrary;
namespace BackgroungWorker__HelloWorld
{
public partial class MainForm : Form
{
ProgressForm f = new ProgressForm();
public MainForm()
{
InitializeComponent();
}
int count = 0;
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (f != null)
{
f.ProgressBar.Value = e.ProgressPercentage;
}
++count;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
MessageBox.Show("The task has been cancelled");
}
else if (e.Error != null)
{
MessageBox.Show("Error. Details: " + (e.Error as Exception).ToString());
}
else
{
MessageBox.Show("The task has been completed. Results: " + e.Result.ToString());
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (f == null)
{
f = new ProgressForm();
}
f.ShowDialog();
//backgroundWorker1.ReportProgress(100);
MyClass.LongOperation();
f.Close();
}
private void btnStart_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void btnCancel_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
this.Close();
}
}
}
Tôi không tìm cách để cập nhật các ProgressBar.
Tôi nên đặt backgroundWorker1.ReportProgress()
ở đâu và tôi nên gọi điều này như thế nào?
Tôi không được thực hiện bất kỳ thay đổi nào trong MyClass. Coz, tôi không biết điều gì sẽ xảy ra hoặc mất bao lâu để hoàn thành hoạt động trong lớp ứng dụng của tôi.
Có ai có thể giúp tôi không?
Tôi không được thực hiện bất kỳ thay đổi nào trong MyClass. Coz, tôi không biết điều gì sẽ xảy ra trong lớp ứng dụng của tôi. – anonymous
Sau đó, bạn không thể báo cáo chính xác tiến độ. Tôi có nghĩa là bạn có thể thêm một bộ đếm thời gian vào 'ProgressForm' để chỉ tăng thanh tiến trình mỗi giây, nhưng điều đó sẽ chỉ cung cấp một ảo tưởng. Nếu bạn không có cách nào để tìm ra tiến trình * thực tế *, thì thanh tiến trình hữu ích như thế nào? –