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 6180271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:49:01+00:00 2026-05-24T00:49:01+00:00

Using code to hook the console close event from this thread , I sometimes

  • 0

Using code to hook the console close event from this thread, I sometimes get a NullReferenceException with no stacktrace (most of the times I don’t). It happens in both release and debug and “break when an exception is thrown” doesn’t help (it breaks, but the stack trace is still empty). I never get this exception when I exit my application normally (which is hitting enter and thus releasing a Console.ReadLine). The Application event log has 2 entries:

Application: MyApp.exe Framework Version: v4.0.30319 Description:
The process was terminated due to an unhandled exception. Exception
Info: System.NullReferenceException Stack:

And:

Faulting application name: Gateway.exe, version: 1.0.0.0, time stamp:
0x4e284101 Faulting module name: unknown, version: 0.0.0.0, time
stamp: 0x00000000 Exception code: 0xc0000005 Fault offset: 0x004d41ce
Faulting process id: 0xf00 Faulting application start time:
0x01cc47b827e19a6e Faulting application path:
C:\dev\MyApp.exe Faulting module path:
unknown Report Id: 689c1caa-b3ab-11e0-ba1b-00247e777f12

Google has revealed some bugs and issues with SetConsoleCtrlHandler, so I’m wondering if this is a lost battle.

  • 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-24T00:49:02+00:00Added an answer on May 24, 2026 at 12:49 am

    The most typical issue with code like that is not keeping a reference to the delegate instance. The one you pass as the first argument to SetConsoleCtrlHandler(). The garbage collector cannot see references held to a delegate object by unmanaged code. So this will, eventually, bomb when the garbage collector runs:

     SetConsoleCtrlHandler(Handler, true);
    

    which is the exact same thing as

     SetConsoleCtrlHandler(new EventHandler(Handler), true);
    

    assuming you used the types in the linked code. The author of that code carefully avoided this issue by making _handler a static variable. As opposed to the temporary delegate instance that is created by the previous two lines of code. Storing it in a static variable ensures it stays referenced for the life of the program. The Right Thing to do in this particular case since you are actually interested in the events until the program ends.

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

Sidebar

Related Questions

I am using pseudo-code here, but this is in JavaScript. With the most efficient
I'm using code that I found on the CodeProject.com for a low-level keyboard hook
I have been using code similar to this MessageDlg('', mtWarning, [mbOK], 0); throughout my
Using the code below, I am returning an nvarchar field from MS SQL 2005
This link discusses how a shutdown hook can be implemented using VB. Now my
Trying to write a GLOBAL CBT HOOK, this is My code, but my hooking
I can color keywords in emacs using the following lisp code in .emacs: (add-hook
I've recently started using code coverage tools (particularily Emma and EclEmma), and I really
I'm using code that takes a bitmap and converts it to 24 BPP so
using the Code Snippet for sending email in VB.Net I have successfully sent an

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.