我有Add button click event
如何添加文件:
private void btnAddfiles_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach (String file in openFileDialog1.FileNames)
{
System.IO.Stream stream;
try
{
if ((stream = openFileDialog1.OpenFile()) != null)
{
using (stream)
{
StartBackgroundFileChecker(file);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
}
我传递的每个文件都StartBackgroundFileChecker(string file)
需要打开进程并在添加到我之前检查此文件,ListBox
所以我这样做BackgroundWorker
是为了防止我的 GUI 冻结并且一切正常:
private void StartBackgroundFileChecker(string file)
{
ListboxFile listboxFile = new ListboxFile();
listboxFile.OnFileAddEvent += listboxFile_OnFileAddEvent;
BackgroundWorker backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.DoWork +=
(s3, e3) =>
{
//check my file via another class
};
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
(s3, e3) =>
{
///
});
backgroundWorker.RunWorkerAsync();
}
当我读完所有文件时,我想更新我的 UI,所以如果我把这个 Ui 更新放在里面backgroundWorker.RunWorkerCompleted...
,在每次调用这个函数后更新我的 UI,我正在寻找在所有调用结束时执行它的方法