Application.Quit()不退出应用程序

时间:2012-12-28 14:48:11

标签: c# monodevelop gtk#

我在C# GTK#中使用了这段简单的代码:

Main.cs

using System;
using Gtk;

namespace Apu{
    class MainClass{
        public static void Main(string[] args){
            Application.Init();
            new ShowForm();
            Application.Run();
        }
    }
}

ShowForm.cs

public partial class ShowForm: Gtk.Window{  
    public ShowForm(): base(Gtk.WindowType.Toplevel){
        MessageDialog md = new MessageDialog(
            this, 
            DialogFlags.DestroyWithParent, 
            MessageType.Error,
            ButtonsType.None,
            "Test"
        );

        md.SetPosition(Gtk.WindowPosition.CenterAlways);
        md.Title = "Test window";
        md.AddButton("Don't stop", ResponseType.Ok);
        md.AddButton("Stop", ResponseType.Cancel);

        ResponseType result = (ResponseType)md.Run();

        if (result.Equals(ResponseType.Cancel)) {
            Console.WriteLine("Quit!");
            md.DestroyEvent += delegate {
                Application.Quit();
            };
            /*md.DeleteEvent += delegate {
                Application.Quit();
            };*/
        }

        md.Destroy();
    }
}

控制台输出Quit!,但程序未退出。 DestroyEventDeleteEvent都不起作用。有谁能解释为什么?这是我第一次使用c#gtk#中的第一个应用。我使用monodevelop作为我的IDE。

修改

Application.Exit()会出错:Gtk.Application does not contain a definition for 'Exit'

2 个答案:

答案 0 :(得分:5)

如果您想关闭该过程,请尝试使用Environment.Exit(0)

答案 1 :(得分:0)

尝试:

md.Destroyed += delegate {
    Application.Quit();
};