Загрузка данных в BackgroundWorker . Используйте BackroundWorkers ProgressChanged
-event, чтобы показать сообщение о прогрессе.
// Show here your wait message make a ProgressBar visible
Mouse.OverrideCursor = Cursors.Wait; // Maybe you only want to set the cursor of the window. Then change this line code (and the reset)
BackgroundWorker bgWorker = new BackgroundWorker() { WorkerReportsProgress=true};
bgWorker.DoWork += (s, e) => {
// Load here your file/s
// Use bgWorker.ReportProgress(); to report the current progress
};
bgWorker.ProgressChanged+=(s,e)=>{
// Here you will be informed about progress and here it is save to change/show progress. You can access from here savely a ProgressBars or another control.
};
bgWorker.RunWorkerCompleted += (s, e) => {
// Here you will be informed if the job is done. Close here your wait message or hide your progressbar
Mouse.OverrideCursor = null;
};
bgWorker.RunWorkerAsync();
Поскольку операция загрузки теперь выполняется асинхронно, вы должны убедиться, что ваш пользовательский интерфейс не позволяет пользователю делать недопустимые действия в текущем состоянии приложения (загрузка). Самая простая возможность сделать это - отключить окно с параметром IsEnabled
, установленным на false
.