Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6659407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:01:51+00:00 2026-05-26T02:01:51+00:00

I thought my code was safe, but it is not. I am using a

  • 0

I thought my code was safe, but it is not.

I am using a form to display some messages that are created and managed somewhere else in my app.

To get to this message I am using a delegate that I add to my external event in the form.load event.

private void ErrorLog_Load( object sender, EventArgs e )
{
    //error handler has been properly initialized in the ctor
    Handler.getInstance( ).errorOccured += errorHandler;
}

I the callback of the form I need to invoke:

protected void handleError( SenderInfo sender, ErrorEventArgument argument )
{
    if (this.InvokeRequired)
    {
        this.Invoke(errorHandler, new object[] { sender, argument });
    }
    else
    {
       //update the form
    }
}

And in the closed/ closing event the callback is unregistered.

Now I have a situation where the window is created as an mdi child an obviously not shown (hidden by another mdi window) and the app crashes in handleError at the line with Invoke with the message that the window handle has not been created.

So three questions:
At which point in time is the window handle of the form created
Appart from adding this.IsHandleCreated to my handleError how should I improve my code
What is the correct event to unregister the callback?

TIA

Mario

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:01:52+00:00Added an answer on May 26, 2026 at 2:01 am

    I think the handle is meant to be created when the HandleCreated event is called (available in .NET 3.5 but it is hidden in the designer), but I don’t think that’s exactly true as you also need to get this.Handle. I saw a better event in the .NET 4 docs but I don’t know for sure about that. The most stable way I’ve come up with in v3.5 is something like this in the constructor, but the window seems to flash up so not sure if this will work without the window ever becoming visible:

    public ErrorLog() 
    {
        InitializeComponent();
    
        this.HandleCreated += new EventHandler((sender, args) =>
        {
            var handle = this.Handle;
            // handle is _probably most likely_ created so now hook up the event etc
            ...
        }
    }
    

    I suppose you would unregister your callback in the FormClosing event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I modified this code from somewhere but I am not sure if I am
Greetings I was doing some lazy initialization code today, and thought why not use
I thought this code would work, but the regular expression doesn't ever match the
I thought people would be working on little code projects together, but I don't
I thought the following line of code should work fine: $(.1).attr('href', '#Home'); Right? But
here's the main code at first I thought is was the message box but
I've got this really simple piece of code that I thought was the correct
I'm using T4 to generate some class definitions and find that I'm getting an
I'm sure this is not a very pythonic situation. But I'm not actually using
I have an intermittent problem with some code that writes to a Windows Event

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.