I have a base WinForm that loads some data on the Load event.
There is a try catch and on the catch I have the following code:
BeginInvoke(new MethodInvoker(Close));
The problem is that the inherited forms "Load" event still gets called.
I also can't just call "Invoke" because I get an error that says "Close" cannot be called while doing "CreateHandle".
Even wrapping this with "this.IsHandleCreated" does not help.
How to I stop the loading of the inherited form and close the inherited form when there is an error in the base "Load" event?
UPDATED WITH MORE CODE
I thought there was enough information listed above to explain the issue but I guess it needs more "example" code, so here goes...
HERE IS THE BASE FORM
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
}
}
}
HERE IS THE TEST FORM INHERITED FROM BASE FORM
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();
}
}