I have found this : How to show a loading graphic/animation when wpf data binding is taking place
But don't understand how to apply it for my work.
I have My MainWindow. It call all my user controls in it.
On a user control, I have a DataGrid. after press "Go" button, the datagrid load data from MySQL. It take a long time to do and I want to show a dialog window with "Please Wait" during the loading of the datagrig.
I have found the link below but don't understant how to call it correctly.
Do I need to put this "loader" in a new class file like "Loader.cs" andthe button call it ? Ok but how to close it when datagrid is finished ?
I'm lost... Well if an other solution exist and simply to use...
Thanks by advance
EDIT TEST 1 :
Tried an simple test with slider to get a time to wait and a button.
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace TEST_LoadingPage
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnTester_Click(object sender, RoutedEventArgs e)
{
Window waitWindow = new Window { Height = 100, Width = 200, WindowStartupLocation = WindowStartupLocation.CenterScreen, WindowStyle = WindowStyle.None };
waitWindow.Content = new TextBlock { Text = "Veuillez patienter...", FontSize = 30, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate
{
Dispatcher.BeginInvoke(new Action(delegate { waitWindow.ShowDialog(); }));
lbReussite.Visibility = Loop.Pause(slider.Value);
Dispatcher.BeginInvoke(new Action(delegate() { waitWindow.Close(); }));
};
worker.RunWorkerAsync();
}
}
public class Loop
{
public static System.Windows.Visibility Pause(double duree)
{
try
{
DateTime begin = new DateTime();
DateTime end = new DateTime();
int i = 0;
begin = DateTime.Now;
end = DateTime.Now.AddSeconds(duree);
while (DateTime.Now <= end)
i++;
return System.Windows.Visibility.Visible;
}
catch (Exception)
{
return System.Windows.Visibility.Hidden;
}
}
}
}
but don't work with error :
The calling thread can not access this object because a different thread owns it
I know its a courant error but I don't see "DispatcherTimer" or else that I've seen before in precedent project... So I'll try the second code tomorrow BUT. I don't understand where i put my method :(
EDIT 2
I tried with your code... I'm too stupide maybe.
I've write the class in a Loader.cs and in my MainWiondow (its a test)
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnTester_Click(object sender, RoutedEventArgs e)
{
Loader<List<Donnees>> loader = new Loader<List<Donnees>>(GenerateList((int)slider.Value);
loader.JobFinished += new Loader<Donnees>.OnJobFinished(loader_JobFinished);
loader.Load();
}
private List<Donnees> GenerateList(int number)
{
List<Donnees> list = new List<Donnees>();
for (int i = 1; i <= number; i++)
{
Donnees data = new Donnees();
data.Id = i;
data.Name = "Ligne " + i;
list.Add(data);
}
return list;
}
void loader_JobFinished(object sender, List<Donnees> result)
{
result = GenerateList((int)slider.Value);
dgDataGrid.ItemsSource = result;
}
}
public class Donnees
{
#region Properties
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
#endregion
public Donnees()
{
id = -1;
name = "";
}
}