You should write your code in the Window_Load
event:
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("Test");
}
EDIT: To work with longer operations like (used a function with more than 10 function inside as you wanted) you could use ThreadPool
like this:
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
ThreadPool.QueueUserWorkItem(_ =>
{
//Longer Process (//set the operation in another thread so that the UI thread is kept responding)
Dispatcher.BeginInvoke(new Action(() =>
{
//use the Dispatcher to "return" to the UI thread
//To change the UI elements like label you should put them here like : label1.Text = "";
}));
});
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…