C# 无法在关闭方法中使用异步

C# 无法在关闭方法中使用异步,c#,wpf,C#,Wpf,我创建了一个名为Instance的方法,它允许我拥有Settings窗口的单个实例,如下所示: public static async Task<Settings> Instance() { if (AppWindow == null) { AppWindow = new Settings(); AppWindow.Closing += async (x, y) =>

我创建了一个名为
Instance
的方法,它允许我拥有
Settings
窗口的单个实例,如下所示:

    public static async Task<Settings> Instance()
    {
        if (AppWindow == null)
        {
            AppWindow = new Settings();

            AppWindow.Closing += async (x, y) =>
            {
                bool close = await AppWindow.CheckSettings();
                y.cancel = (close) ? true : false;
                AppWindow = null;
            };
        }

        return AppWindow;
    }

问题在于,
Cancel
没有等待CheckSettings()

在调用
async
方法之前,将
Cancel
属性设置为
true

public static Settings Instance
{
    get
    {
        if (AppWindow == null)
        {
            AppWindow = new Settings();
            //attach the event handler
            AppWindow.Closing += AppWindow_Closing;
        }
        return AppWindow;
    }
}

private static async void AppWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    e.Cancel = true;

    //call the async method
    bool close = await AppWindow.CheckSettings();
    if (close)
    {
        AppWindow win = (AppWindow)sender;
        //detach the event handler
        AppWindow.Closing -= AppWindow_Closing;
        //...and close the window immediately
        win.Close();
        AppWindow = null;
    }
}

在调用
async
方法之前,将
Cancel
属性设置为
true

public static Settings Instance
{
    get
    {
        if (AppWindow == null)
        {
            AppWindow = new Settings();
            //attach the event handler
            AppWindow.Closing += AppWindow_Closing;
        }
        return AppWindow;
    }
}

private static async void AppWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    e.Cancel = true;

    //call the async method
    bool close = await AppWindow.CheckSettings();
    if (close)
    {
        AppWindow win = (AppWindow)sender;
        //detach the event handler
        AppWindow.Closing -= AppWindow_Closing;
        //...and close the window immediately
        win.Close();
        AppWindow = null;
    }
}

你什么都不做
async
为什么要异步?@nvoigt我需要异步,因为在CheckSettings中我有一些Mahapp框架的方法,比如wait ShowMessageAsync(),Instance()方法中没有异步调用,而且,afaik async EventHandler对于CancelEventHandler是不可行的,因为事件将被调用,然后在处理程序完成之前继续,所以设置Cancel属性不会有太大作用。@nvoigt检查更新您不做任何事情
async
那么为什么要让它异步?@nvoigt我需要异步,因为在CheckSettings中我有一些Mahapp框架的方法,比如wait ShowMessageAsync(),Instance()方法中没有异步调用,此外,afaik async EventHandler对于CancelEventHandler不可行,因为事件将在处理程序完成之前被调用并继续,因此设置Cancel属性不会有多大作用。@nvoigt检查更新OP在调用该方法之前不知道是否要取消关闭。该方法的全部要点是确定他们是否应该取消表单的关闭。你现在无法关闭此表单。是吗?仅当CheckSettings返回true时,窗口才会关闭。你的意思是什么?然后它将再次被取消,并且表单将永远不会关闭。不。因为一旦CheckSettings方法返回true,事件处理程序就会分离。然后按预期关闭“表单”。在进行否决投票之前,您应该尝试代码(或者至少仔细阅读代码)。OP在调用该方法之前不知道是否要取消关闭。该方法的全部要点是确定他们是否应该取消表单的关闭。你现在无法关闭此表单。是吗?仅当CheckSettings返回true时,窗口才会关闭。你的意思是什么?然后它将再次被取消,并且表单将永远不会关闭。不。因为一旦CheckSettings方法返回true,事件处理程序就会分离。然后按预期关闭“表单”。在否决投票之前,您应该尝试代码(或者至少仔细阅读一点)。
public static Settings Instance
{
    get
    {
        if (AppWindow == null)
        {
            AppWindow = new Settings();
            //attach the event handler
            AppWindow.Closing += AppWindow_Closing;
        }
        return AppWindow;
    }
}

private static async void AppWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    e.Cancel = true;

    //call the async method
    bool close = await AppWindow.CheckSettings();
    if (close)
    {
        AppWindow win = (AppWindow)sender;
        //detach the event handler
        AppWindow.Closing -= AppWindow_Closing;
        //...and close the window immediately
        win.Close();
        AppWindow = null;
    }
}