I have a number of time-consuming tasks in my program such as loading large DataGrids, updating multiple Databases etc. etc. What I would like to do is display a "Please wait..." MessageBox or Form whilst these tasks are running. I've had a look at BackgroundWorker but can't seem to get my head round it.
To minimize the amount of code needed, ideally I would like a method that can be called from anywhere as this will be used in many places in my program, though I know that might be tricky.
public CompanyManagement()
{
InitializeComponent();
FillDataGrid(); // This method takes all the time
}
private void FillDataGrid()
{
//Display a message until this has completed
var _cDS = new CompanyDataService();
var Companies = new ObservableCollection<CompanyModel>();
Companies = _cDS.HandleCompanySelect();
CompanyICollectionView = CollectionViewSource.GetDefaultView(Companies);
CompanyICollectionView.SortDescriptions.Add(new SortDescription("CompanyName", ListSortDirection.Ascending));
DataContext = this;
cancelButton.Visibility = Visibility.Hidden;
if (compNameRad.IsChecked == false &&
compTownRad.IsChecked == false &&
compPcodeRad.IsChecked == false)
{
searchBox.IsEnabled = false;
}
dataGrid.SelectedIndex = 0;
}
Is there a way to have a completely separate method to call? I guess this would be hard as there is no way then for that method to know when the tasks have been completed, unless it is surrounding the code to be checked.
EDIT: using Matthew's example;
public CompanyManagement()
{
InitializeComponent();
var wait = new PleaseWait("My title", "My Message", () => FillDataGrid());
wait.ShowDialog();
}