31 people like it.
Like the snippet!
Exiting cleanly
Demonstrates how to run a function when the user presses Ctrl+C, closes the console window, or logs off.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
|
open System.Runtime.InteropServices
open System.Threading
type ConsoleCtrlDelegate = delegate of int -> bool
[<DllImport("kernel32.dll")>]
extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate handlerRoutine, bool add)
let cleanup () =
printfn "Exiting..."
[<EntryPoint>]
let main _ =
let handler = ConsoleCtrlDelegate(fun _ -> cleanup (); false)
ignore (SetConsoleCtrlHandler(handler, true))
try
printfn "Press Ctrl+C to exit"
Thread.Sleep Timeout.Infinite
0
finally
ignore (SetConsoleCtrlHandler(handler, false))
|
namespace System
namespace System.Runtime
namespace System.Runtime.InteropServices
namespace System.Threading
type ConsoleCtrlDelegate =
delegate of int -> bool
Full name: Script.ConsoleCtrlDelegate
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
type bool = System.Boolean
Full name: Microsoft.FSharp.Core.bool
Multiple items
type DllImportAttribute =
inherit Attribute
new : dllName:string -> DllImportAttribute
val EntryPoint : string
val CharSet : CharSet
val SetLastError : bool
val ExactSpelling : bool
val PreserveSig : bool
val CallingConvention : CallingConvention
val BestFitMapping : bool
val ThrowOnUnmappableChar : bool
member Value : string
Full name: System.Runtime.InteropServices.DllImportAttribute
--------------------
DllImportAttribute(dllName: string) : unit
val SetConsoleCtrlHandler : handlerRoutine:ConsoleCtrlDelegate * add:bool -> bool
Full name: Script.SetConsoleCtrlHandler
val handlerRoutine : ConsoleCtrlDelegate
val add : bool
val cleanup : unit -> unit
Full name: Script.cleanup
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Multiple items
type EntryPointAttribute =
inherit Attribute
new : unit -> EntryPointAttribute
Full name: Microsoft.FSharp.Core.EntryPointAttribute
--------------------
new : unit -> EntryPointAttribute
val main : string [] -> int
Full name: Script.main
val handler : ConsoleCtrlDelegate
val ignore : value:'T -> unit
Full name: Microsoft.FSharp.Core.Operators.ignore
Multiple items
type Thread =
inherit CriticalFinalizerObject
new : start:ThreadStart -> Thread + 3 overloads
member Abort : unit -> unit + 1 overload
member ApartmentState : ApartmentState with get, set
member CurrentCulture : CultureInfo with get, set
member CurrentUICulture : CultureInfo with get, set
member DisableComObjectEagerCleanup : unit -> unit
member ExecutionContext : ExecutionContext
member GetApartmentState : unit -> ApartmentState
member GetCompressedStack : unit -> CompressedStack
member GetHashCode : unit -> int
...
Full name: System.Threading.Thread
--------------------
Thread(start: ThreadStart) : unit
Thread(start: ParameterizedThreadStart) : unit
Thread(start: ThreadStart, maxStackSize: int) : unit
Thread(start: ParameterizedThreadStart, maxStackSize: int) : unit
Thread.Sleep(timeout: System.TimeSpan) : unit
Thread.Sleep(millisecondsTimeout: int) : unit
type Timeout =
static val Infinite : int
Full name: System.Threading.Timeout
field Timeout.Infinite = -1
More information