-1

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();
    }
}
goroth
  • 2,510
  • 5
  • 35
  • 66
  • 1
    The code did not stop running. Throw an exception. – Hans Passant Sep 27 '17 at 02:13
  • You need to show a [mcve] to get a full answer on this. – Enigmativity Sep 27 '17 at 04:11
  • @HansPassant If I "throw" in the base form then it will cause an unhandled exception in the application. I have a global unhandled exception handler but I only want to use it as a last resort. I want to handle the exception inside the base load event and then close the form if possible. – goroth Sep 27 '17 at 13:29
  • @Enigmativity I have added more code / example. – goroth Sep 27 '17 at 13:30
  • 1
    Right, you need to override OnLoad() instead of using the Load event. So you can put try/catch around the base.OnLoad() call. [More here](https://stackoverflow.com/a/3670912/17034). – Hans Passant Sep 27 '17 at 13:38
  • @HansPassant brilliant, if I hear what you are saying... add a try catch in the base form "OnLoad" event, load some data, and only call the "base.OnLoad" if there is no error. Does that sound correct? If this is true, would I even need to call the "close" method in the "OnLoad" event if there is an exception? – goroth Sep 27 '17 at 13:50
  • 1
    It is entirely up to you to interpret the exception. But in general you cannot reasonably allow the form to continue loading when something nasty happened. If it would be easily recoverable then the try/catch would be in the other form's Load event. – Hans Passant Sep 27 '17 at 14:11
  • @HansPassant stackoverflow will not allow me accept a "comment" as the answer. If you want to add a quick answer to this post, I will set it as accepted, if not, with your permission, I will post the full code I created with your help and use that as the accepted answer. – goroth Sep 27 '17 at 14:58
  • Just show us what you ended doing in your own post and mark it as the answer. – Hans Passant Sep 27 '17 at 14:59

2 Answers2

1

With a suggestion from "Hans Passant" linked here Form_Load() 'event' or Override OnLoad()
I am using the "OnLoad" event of the base form to stop the "load" event of the inherited form when there is an exception inside the base form upon loading.

HERE IS THE BASE FORM

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
    }
}
goroth
  • 2,510
  • 5
  • 35
  • 66
0

Avoid calling functions in the Load event. Try using the Shown event instead.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
xyaz
  • 139
  • 8
  • The answer is fine. I don't know what @Enigmativity was talking about. There's nothing wrong with linking to the documentation for a function/event/API where you mention it. – Cody Gray - on strike Sep 27 '17 at 06:28
  • @Huron_rare I would like to use the "shown" event but some of the 3rd party controls I use on the base form have issues when loading / binding data in the "shown" event. – goroth Sep 27 '17 at 13:35
  • You could specify that on the question so users will know how far you tried. – xyaz Sep 27 '17 at 15:14