开发者

C# How to terminate application.run() [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I would like to know, how to terminate a program using, for example, the escape key. In general, what I have to do to stop it after the application.run(..)?

How can I insert this

 private void myForm_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { 
           if (e.KeyCode == Keys.Escape) { 
               App开发者_JAVA百科lication.Exit(); 
            } 
        } 

in the code below

static void Main()
    {

        using (WinForm new_form = new WinForm())
        {
            new_form.InitializeComponent();
            new_form.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
            new_form.InitializeDevice();
            new_form.LoadSurfaces();
            new_form.Set3D();
            Application.Run(new_form);
         }

}


Call the Application.Exit() method.

The Exit method stops all running message loops on all threads and closes all windows of the application. This method does not necessarily force the application to exit. The Exit method is typically called from within a message loop, and forces Run to return. To exit a message loop for the current thread only, call ExitThread.

Exit raises the following events and performs the associated conditional actions:

  • A FormClosing event is raised for every form represented by the OpenForms property. This event can be canceled by setting the Cancel property of their FormClosingEventArgs parameter to true.

  • If one of more of the handlers cancels the event, then Exit returns without further action. Otherwise, a FormClosed event is raised for every open form, then all running message loops and forms are closed.

To do this when the Esc key is pressed, you might want to handle the KeyUp event for your form:

private void myForm_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape)
    {
        Application.Exit();
    }
}


If you're using WinForms then go for Application.Exit().

If you're using WPF then use Application.Current.Shutdown();


Just call Application.Exit() or close all active forms. E.g. you could simply add Close(); to the KeyDown event handler in your form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜