Run program after wpf windows form appears

后端 未结 1 676
余生分开走
余生分开走 2020-12-21 13:35

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

namespace WpfApplication1
{
    /// 
    /// Interaction logic for MainWindow.xa         


        
相关标签:
1条回答
  • 2020-12-21 14:19

    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 = ""; 
            }));
        });
    }
    
    0 讨论(0)
提交回复
热议问题