Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

c# - Run program after wpf windows form appears

Hello i have problem with wpf (c#) project. this is my source

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            MessageBox.Show("Test");
        }
    }
}

i want to run my program

MessageBox.Show("Test");

after windows form appears ... but in this code when i start the program, just first show Test in message box and after that windows form appears ! what should i do to first windows form appears and after that a message box open to show the (Test)? i am using visual studio 2015 (WPF) project

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 = ""; 
        }));
    });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...