我正在嘗試編寫一個簡單的程序(c#),它讀取一組zip文件以搜索某些特定文件。進度條不按最大尺寸進度
我實現了這個使用winform和後臺工作,但我有一些麻煩了解如何配置進度條根據我解析文件的數量動態進步。
例如在目錄A中我有400個zip文件,所以我希望進度條的「大小」爲400個單位,因此打開的每個文件都會將進度條增加1.在目錄B中,我只有4個zip文件,所以我需要4塊在進度欄
我試圖做下面的代碼,只是爲了測試進度條 我設置最大值爲20(代表20個zip文件),並在一個循環遞增1進度酒吧
private void button_SearchZip_Click(object sender, EventArgs e)
{
if(!backgroundWorker_SearchZip.IsBusy)
{
SearchZipArgs args = new SearchZipArgs
{
sourceDirectory = this.textBox_SrcDir.Text
};
backgroundWorker_SearchZip.RunWorkerAsync(args);
this.button_SearchZip.Enabled = false;
}
else
{
MessageBox.Show(@"Search already in process. please try again later");
}
}
private void backgroundWorker_SearchZip_DoWork(object sender, DoWorkEventArgs e)
{
this.progressBar_SearchZip.Style = ProgressBarStyle.Blocks;
//this.progressBar_SearchZip.Step = 1;
this.progressBar_SearchZip.Minimum = 0;
this.progressBar_SearchZip.Maximum = 20;
for(int i = 0; i < 20; i++)
{
backgroundWorker_SearchZip.ReportProgress(i);
Thread.Sleep(300);
}
}
public MainForm()
{
InitializeComponent();
this.backgroundWorker_SearchZip.WorkerReportsProgress = true;
this.backgroundWorker_SearchZip.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker_SearchZip_DoWork);
this.backgroundWorker_SearchZip.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker_SearchZip_ProgressChanged);
this.backgroundWorker_SearchZip.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker_SearchZip_RunWorkerCompleted);
}
private void backgroundWorker_SearchZip_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar_SearchZip.Value = e.ProgressPercentage;
}
,但是這是我的進度條得到:
出於某種原因,如果取消註釋:
this.progressBar_SearchZip.Step = 1;
進度條完全不
工作的任何幫助將是不錯:)
編輯: 發現問題!我試圖從後臺線程中更改進度條,並且出現錯誤「跨線程操作無效:控制'progressBar Search Zip'從修改此錯誤後創建的線程以外的線程訪問」 在此線程的幫助下:Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on)問題已解決
backgroundWorker.ProgressChanged事件在哪裏? – A3006
@ A3006忘了添加它。我編輯了問題 – Dardar