1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
namespace WpfThreading2
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MyMainWindow : Window
{
SplashScreen splash = new SplashScreen();
public MyMainWindow()
{
InitializeComponent();
this.DataContext = this;
splash.DataContext = this;
splash.Show();
}
public string TexteDeroulement
{
get { return (string)GetValue(TexteDeroulementProperty); }
set { SetValue(TexteDeroulementProperty, value); }
}
// Using a DependencyProperty as the backing store for TexteDeroulement. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TexteDeroulementProperty =
DependencyProperty.Register("TexteDeroulement",
typeof(string), typeof(MyMainWindow),
new FrameworkPropertyMetadata("Data is Loading ....!"));
public void LongTask()
{
// Create and start a new thread
Thread newThread = new Thread(
new ThreadStart(
delegate
{
Thread.Sleep(2000);//permet de persister l'affichage initial
Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new Action<string>(SetValueMsg), "Patienter Data is loaded...!");
Thread.Sleep(2000); //simuler tache longue ,en fait depend de SetValueMsg
Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new Action<string>(SetValueMsg), "Termine..App starting ...!");
Thread.Sleep(2000);//idem
Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new Action(SetValueClose));//ferme splash
}
));
newThread.SetApartmentState(ApartmentState.STA);
newThread.IsBackground = true;
newThread.Start();
// WPF automatically creates a new Dispatcher to manage our new thread. All we have to
// do to make our thread functional is ask this Dispatcher to start running
System.Windows.Threading.Dispatcher.Run();
}
private void SetValueMsg(string msg)
{
this.TexteDeroulement = msg;
}
private void SetValueClose()
{
splash.Close();
splash = null;
this.Show();
}
}
} |
Partager