如何将我的表格后,开始执行代码? [英] How can I execute code after my form starts?

查看:106
本文介绍了如何将我的表格后,开始执行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是真正的新的Windows编程和不太清楚什么是去了解编程的正确方法。

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

这是我的困惑。

我有一个单一的形式:

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


    }

和我的Program.cs的类我有以下代码:

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

        }


    }

现在,一旦窗口已经由Application.Run命令显示,该程序暂停,在该点。我怎么可以做进一步的处理,同时在窗口呢?

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?

以上只是一个例子。我的目的是读取XML文件并显示一个DataGridView。随后,我看更改的XML文件,并每次做出改变,我想刷新datagridview的。然而,一旦控制台弹出,我怎么能继续我的程序和更改动态表单上显示的信息?

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?

推荐答案

这之后 Application.Run 处理在窗体的加载事件处理程序通常触发。您可以轻松创建一个加载在Visual Studio 办法通过双击窗体上的任何开放空间。

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.

这会看的的东西的这个样子。

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



究其原因,这是(在其他几个答案说)是主要的线程(即那叫一个 Application.Run(窗口))现在采取与运营消息泵的形式。您可以通过消息继续在该线程运行的东西,通过表单的形式或事件。或者,你可以开始一个新的线程。这可以在main方法来实现,的的调用 Application.Run(窗口),但大多数人会在<$ C $做到这一点C>的Form_Load 或形式构造,保证形式设立,等等。一旦 Application.Run 的回报,的所有形式已经结束。

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.

这篇关于如何将我的表格后,开始执行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆