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
185 views
in Technique[技术] by (71.8m points)

c# - How can I execute code after my form starts?

I'm really new to Windows Forms programming and not quite sure what's the right way to go about programming.

This is my confusion.

I have a single form:

    public partial class ReconcilerConsoleWindow : Form
    {
        public ReconcilerConsoleWindow()
        {
            InitializeComponent();
            SetLogText("Started");

        }

        public void SetLogText(String text)
        {
            string logInfo = DateTime.Now.TimeOfDay.ToString() + ": " + text + Environment.NewLine;
            tbx_Log.AppendText(logInfo);
        }


    }

And in my Program.cs class I have the following code:

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ReconcilerConsoleWindow window = new ReconcilerConsoleWindow();
            Application.Run(window);

            if (CallSomeMethod() == true)
            {
                 window.SetLogText("True");
            }                     

        }


    }

Now, once the window has been displayed by the Application.Run command, the program halts at that point. How can I do further processing while the window is up?

The above is just an example. My purpose is to read an XMl file and display a datagridview. Subsequently, I watch the XMl file for changes and everytime a change is made, I want to refresh the datagridview. However, once the console pops up, how can i continue with my program and make changes to the information displayed on the form on the fly?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Processing that occurs after Application.Run is usually triggered in the form's Load event handler. You can easily create a Load method in Visual Studio by double clicking any open space on the form.

This would look something like this.

    private void ReconcilerConsoleWindow_Load(object sender, EventArgs e)
    {
        if (CallSomeMethod())
        {
            this.SetLogText("True");
        }
    }

The reason this is (as stated in several other answers) is that the main thread (the one that called Application.Run(window)) is now taken up with operating the Message Pump for the form. You can continue running things on that thread through messaging, using the form's or forms' events. Or you can start a new thread. This could be done in the main method, before you call Application.Run(window), but most people would do this in Form_Load or the form constructor, to ensure the form is set up, etc. Once Application.Run returns, all forms are now closed.


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

...