C# - Application.Run()

前端 未结 2 1703
野趣味
野趣味 2021-02-13 09:08

I am just starting .Net development (C#) and have come across some code that has me slightly confused....

If I have

Form myForm = new Form();

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-13 09:54

    Application.Run(myForm); makes that form visible to user. It is the first form which get loaded in memory. And it runs this form in a message loop, so that you get all user events.

    Short Answer:

    Application.Run begins running a standard application message loop on the current thread.

    Long Answer:

    Application.Run causes the windows application enters the message loop within Winmain to process various windows messages the OS posts to a message queue.The message loop, "Loops" until its receives a WM_QUIT message. It uses GetMessage and PeekMessage to retrive messages and PostMessage to sent the retrived messages to Windows procedure.

    If you do

    Form myForm = new Form(); 
    myForm.Show();
    

    it will show the form and exit out. You will use new Form() & .Show() when you want to launch a new form from existing form.

    Hope this answers your question.

提交回复
热议问题