简体   繁体   English

我的表单启动后如何执行代码?

[英]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. 我是Windows Forms编程的新手,并不太确定编程的正确方法。

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: 在我的Program.cs类中,我有以下代码:

    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. 现在,一旦Application.Run命令显示窗口,程序就会在此时停止。 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. 我的目的是读取XMl文件并显示datagridview。 Subsequently, I watch the XMl file for changes and everytime a change is made, I want to refresh the datagridview. 随后,我观察XMl文件的更改,每次进行更改时,我都想刷新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? 但是,一旦控制台弹出,我如何继续我的程序并动态更改表格上显示的信息?

Processing that occurs after Application.Run is usually triggered in the form's Load event handler. Application.Run之后发生的处理通常在表单的Load事件处理程序中触发。 You can easily create a Load method in Visual Studio by double clicking any open space on the form. 通过双击窗体上的任何空白区域,可以在Visual Studio中轻松创建Load方法。

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. 这是(正如其他几个答案中所述)的原因是主线程 (调用Application.Run(window)那个)现在用于操作表单的消息泵 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. 这可以在main方法中完成, 然后再调用Application.Run(window) ,但大多数人会在Form_Load或表单构造函数中执行此操作,以确保表单已设置等。一旦Application.Run返回, 所有表单现已关闭。

Application.Run starts the Windows event handling loop. Application.Run启动Windows事件处理循环。 That loop won't finish til your form closes, at which time anything you do to it won't matter anyway. 该循环将无法完成,直到您的表单关闭,此时您所做的任何事情无论如何都无关紧要。

If you want to do something with your form, do it in the form's Load event handler. 如果要对表单执行某些操作,请在表单的Load事件处理程序中执行此操作。

Program.cs is not meant to have business rules, it should only call your Form and display it. Program.cs并不意味着有业务规则,它应该只调用您的表单并显示它。 All datagrid loading/refreshing/editing should be done at your Forms. 所有数据网格加载/刷新/编辑都应在您的表单中完成。 You should be using the Events defined on Forms class, like: OnLoad, OnUnload, OnClose and many others etc. 您应该使用在Forms类上定义的事件,例如:OnLoad,OnUnload,OnClose和许多其他等。

You are missing the concept. 你错过了这个概念。 In a Windows Forms Application, your Main Thread is responsible for running the Form. 在Windows窗体应用程序中,主线程负责运行窗体。

You can always use more Threads, but in Windows Forms I would recommend using a BackgroundWorker Component for parallel Tasks: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx 您总是可以使用更多的线程,但在Windows窗体中,我建议使用BackgroundWorker组件来执行并行任务: http//msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

Or a Timer: http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx 或计时器: http//msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

Once Application.Run(window) is called, you'll want to handle subsequent things within the application window itself. 调用Application.Run(窗口)后,您将需要处理应用程序窗口本身内的后续内容。

In the code view of the form, find the following (or add it) 在窗体的代码视图中,找到以下(或添加它)

private void ReconcilerConsoleWindow_Load(object sender, EventArgs e)
{
//this is where you do things :)
            if (CallSomeMethod() == true)
            {
                 window.SetLogText("True");
            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM