so I am relatively new to the C#/WPF techstack. I created a WPF program but sometimes the main window takes a bit to load and I am trying to add a loading window with progress bar but the UI is not updating as progress goes its just doing it all at once at the end of the process.
I've tried using a background worker adding some of the tasks that need to be run to it and running it ASYNC but to report the progress I need to add the worker to the class doing the work and the window reporting it and its causing a circular issue where you need the worker to get the datahandler but you need the login window to get the worker and you need the datahandler to get the login window.
So I tried creating and showing the loading window in mainWindow's constructor, and updating the progressbar element from there using await Task.Run(()=> this.Dispatcher.Invoke((Action)(() => { LDNG.LoadingBar.Value = percent; }))); but it doesnt update it as program runs and just does everything at the end when the main window is basically completed
heres a snippet to give an idea of the current state of my code
DataHandler dataCtrl;
public MainWindow(string version, string username, string password)
{
displayProgress();
dataCtrl = new DataHandler(username, password);
updateProgress(10);
InitializeComponent();
updateProgress(30);
signatures.Add(dataCtrl.user);
updateProgress(40);
MainWindowContainer.Title = "SDS2 " + DesignData.SDS2.Database.Version.ProgramVersion.ToString() + " Transmittal Report Tool";
updateProgress(50);
initialBinding();
updateProgress(100);
}
async void displayProgress() {
this.LDNG = new ProgressBarWindow(ref worker);
await Task.Run(() => this.Dispatcher.Invoke((Action)(() => { LDNG.Show(); })));
}
async void updateProgress(int percent) {
//LDNG.LoadingBar.Value = percent
await Task.Run(()=> this.Dispatcher.Invoke((Action)(() => { LDNG.LoadingBar.Value = percent; })));
//MessageBox.Show("");
//if (percent == 100) {
// LDNG.Close();
//}
}
void initialBinding() {
RepoBtn.Content = dataCtrl.getDefaultRepo("initialbinding mainwindow");
JobBtn.Content = dataCtrl.getDefaultJob();
InputByCmbx.ItemsSource = signatures;
//updateProgress((int)(50*(1/21)+50));
InputCheckedByCmbx.ItemsSource = signatures;
//updateProgress((int)(50*(2 /21)+50));
... some more code here
}
if you've got a suggestion for a better way to go about adding this loading screen I'll take that too I just need to get ere done