我不是c#的專家,但我想要做的是更新後臺工作進度條。我正在使用以下代碼:C#ProgressBar backgroundworker中的數據綁定
progressBar1.DataBindings.Add("Value", _dm, "Progress", true,
DataSourceUpdateMode.OnPropertyChanged);
在GUI線程上執行時沒有後臺工作程序時可以使用。 Progress屬性是一個更新(使用INotifyPropertyChanged)來自另一個背景工作者(我無權訪問)的進度的屬性。
我該如何使它工作,以便使用backgroundworker更新而不是將它全部放在GUI線程上?
我的代碼(簡化):
class DownloadManager : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private double _progressValue;
public double Progress
{
get { return _progressValue; }
private set
{
if (!value.Equals(_progressValue))
{
_progressValue = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Progress"));
}
}
}
public void Download()
{
var downloader = new Downloader();
downloader.DownloadProgressChanged += (sender, e)
=> Progress = e.ProgressPercentage;
downloader.Execute();
}
}
public partial class MainForm
{
private readonly DownloadManager _dm;
public MainForm()
{
InitializeComponent();
_dm = new DownloadManager();
}
private void btnDownload_Click(object sender, EventArgs e)
{
//TRIED HERE ...
progressBar1.DataBindings.Add("Value", _dm, "Progress", true,
DataSourceUpdateMode.OnPropertyChanged);
bwDownload.RunWorkerAsync();
}
}
private void bwDownload_DoWork(object sender, DoWorkEventArgs e)
{
//AND TRIED HERE
progressBar1.DataBindings.Add("Value", _dm, "Progress", true, DataSourceUpdateMode.OnPropertyChanged);
//THIS AINT WORKING EITHER
if (progressBar1.InvokeRequired) {
progressBar1.Invoke(new MethodInvoker(()
=> progressBar1.DataBindings.Add("Value", _dm, "Progress", true,
DataSourceUpdateMode.OnPropertyChanged)));
}
_dm.Download();
}
}
不工作或者..我忘了將它添加到我的簡化代碼 – JC97