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

  • SEARCH
  • Home
  • 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 3724590
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:41:45+00:00 2026-05-19T06:41:45+00:00

I would like to have a certain function executed when a console application exits.

  • 0

I would like to have a certain function executed when a console application exits. I have found many solutions but none of them work for me. Why the following code does not display CancelKeyPress etc?

printfn "Starting a Test"
System.Console.ReadLine() |> ignore
System.Console.CancelKeyPress.Add (fun _ -> printfn "CancelKeyPress" )
System.AppDomain.CurrentDomain.ProcessExit.Add (fun _ -> printfn "ProcessExit" )
System.AppDomain.CurrentDomain.DomainUnload.Add (fun _ -> printfn "DomainUnload" )

I have slightly modified my code and added the try finally statement but without any luck. I run the application and then hit “ctrl + c” or click on “Close button”

let write v = System.IO.File.AppendAllText("test.txt", v + "\n")
try
  write "Starting a Test 2"
  System.Console.ReadLine() |> ignore
  System.Console.CancelKeyPress.Add (fun _ -> write "CancelKeyPress" )
  System.AppDomain.CurrentDomain.ProcessExit.Add (fun _ -> write "ProcessExit" )
  System.AppDomain.CurrentDomain.DomainUnload.Add (fun _ -> write "DomainUnload" )
finally
  write "Try Finally"
  • 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-19T06:41:47+00:00Added an answer on May 19, 2026 at 6:41 am

    When I run your example it prints “ProcessExit”, so that one works. The reason why “CancelKeyPress” is not printed is because the application probably terminates before it occurs (And you also need to register the handler before the ReadLine). The following will cancel first 10 Ctrl + C presses and then exit on the next one:

    Console.CancelKeyPress.Add(fun arg -> 
      printfn "CancelKeyPress"; arg.Cancel <- true ) 
    for i in 0 .. 10 do 
      System.Console.ReadLine() |> ignore 
    

    Anyway one straightforward option that should work would be to wrap the whole main function inside try .. finally. Something like:

    let main (args) = 
      try
        // run the application
      finally
        // finalization code here
    

    EDIT When I run your second example, I get a file with:

    Starting a Test 2
    Try Finally
    ProcessExit
    

    I’m not sure why the DomainUnloaded hasn’t been printed, but the remaining should work as expected.

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

Sidebar

Related Questions

I have a set of png images that I would like to process with
I have found a great tutorial on how to show and hide a certain
I would like to use the .replace function to replace multiple strings. I currently
I would like to dynamically modify the values of certain form elements, more specifically
I have now a function like this function checkInput($content, $emptyMessage = ){ if(isset($content)){ return
I have a gross looking function and am wanting to clean it up. All
I would like to create a compiled query which uses reusable where predicates. An
I'd like to write a function that takes both a value constructor for a
I'm working on a solution to make certain data from a large database available
I have a class which I use to record a value in a given

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.