C# Winform基本窗体在加载时关闭继承的窗体事件

C# Winform基本窗体在加载时关闭继承的窗体事件,c#,winforms,inheritance,C#,Winforms,Inheritance,我有一个基本WinForm,可以在加载事件中加载一些数据。 有一个try catch,在catch上我有以下代码: BeginInvoke(new MethodInvoker(Close)); 问题是继承的表单“加载”事件仍然会被调用。 我也不能只调用“Invoke”,因为我在执行“CreateHandle”时遇到一个错误,即“Close”不能被调用。 甚至用“this.IsHandleCreated”来包装也无济于事 当基本“加载”事件中出现错误时,如何停止加载继承的表单并关闭继承的表单 更

我有一个基本WinForm,可以在加载事件中加载一些数据。
有一个try catch,在catch上我有以下代码:

BeginInvoke(new MethodInvoker(Close));
问题是继承的表单“加载”事件仍然会被调用。
我也不能只调用“Invoke”,因为我在执行“CreateHandle”时遇到一个错误,即“Close”不能被调用。
甚至用“this.IsHandleCreated”来包装也无济于事

当基本“加载”事件中出现错误时,如何停止加载继承的表单并关闭继承的表单

更新了更多代码
我认为上面列出的信息足以解释这个问题,但我想它需要更多的“示例”代码,所以这里是

这是基本表格

public partial class BaseForm : Form
{
    public BaseForm()
    {
        InitializeComponent();
    }

    private void BaseForm_Load(object sender, EventArgs e)
    {
        try
        {
            // only run this code in RunTime
            if (LicenseManager.UsageMode == LicenseUsageMode.Runtime)
            {
                // load some data
                LoadData();
            }
        }
        catch (Exception ex)
        {
            // handle the exception and tell the user something BUT don't kill the app... a THROW here could kill the app
            HandleException(ex);

            // since we are loading the form we need to exit the form if there is an error loading.
            BeginInvoke(new MethodInvoker(Close)); // can't close a form on load unless using begin invoke
        }
    }
}
下面是从基本表单继承的测试表单

public partial class TestForm : BaseForm
{
    public TestForm()
    {
        InitializeComponent();
    }

    private void TestForm_Load(object sender, EventArgs e)
    {
        // do some work BUT only if the BASE form loaded correctly
        DoSomething();
    }
}
public partial class BaseForm : Form
{
    public BaseForm()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        try
        {
            // only run this code in RunTime
            if (LicenseManager.UsageMode == LicenseUsageMode.Runtime)
            {
                // load some data
                LoadData();
            }

            // only call the OnLoad event if there is no exception when loading the data of the base form
            base.OnLoad(e);
        }
        catch (Exception ex)
        {
            // handle the exception and tell the user something BUT don't kill the app... a THROW here could kill the app
            HandleException(ex);

            // close the forms
            this.Close();
        }
    }

    private void BaseForm_Load(object sender, EventArgs e)
    {
        // NOTE: any exception handled here will still cause the inherited forms Load event to fire
    }
}

避免在加载事件中调用函数。尝试使用该活动。

此处链接“Hans Passant”的建议
我正在使用基本表单的“OnLoad”事件来停止继承表单的“load”事件,因为加载时基本表单内部出现异常

这是基本表格

public partial class TestForm : BaseForm
{
    public TestForm()
    {
        InitializeComponent();
    }

    private void TestForm_Load(object sender, EventArgs e)
    {
        // do some work BUT only if the BASE form loaded correctly
        DoSomething();
    }
}
public partial class BaseForm : Form
{
    public BaseForm()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        try
        {
            // only run this code in RunTime
            if (LicenseManager.UsageMode == LicenseUsageMode.Runtime)
            {
                // load some data
                LoadData();
            }

            // only call the OnLoad event if there is no exception when loading the data of the base form
            base.OnLoad(e);
        }
        catch (Exception ex)
        {
            // handle the exception and tell the user something BUT don't kill the app... a THROW here could kill the app
            HandleException(ex);

            // close the forms
            this.Close();
        }
    }

    private void BaseForm_Load(object sender, EventArgs e)
    {
        // NOTE: any exception handled here will still cause the inherited forms Load event to fire
    }
}

代码没有停止运行。抛出一个异常。您需要显示一个以获得此问题的完整答案。@HansPassant如果我在基本表单中“抛出”,则它将导致应用程序中出现未处理的异常。我有一个全局未处理的异常处理程序,但我只想将其作为最后手段使用。我想在基本加载事件中处理异常,然后如果可能的话关闭表单。因此,您可以在base.OnLoad()调用周围放置try/catch。这完全取决于您对异常的解释。但是一般来说,当发生了一些不愉快的事情时,您不能合理地允许表单继续加载。如果它很容易恢复,那么try/catch将在另一个表单的Load事件中。答案很好。我不知道@Enigmativity在说什么。链接到您提到的函数/事件/API的文档并没有问题。@Huron_我想使用“Showed”事件,但我在基本表单上使用的一些第三方控件在“Showed”事件中加载/绑定数据时出现问题。您可以在问题上指定,以便用户知道您尝试了多远。