Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run f# scripts without showing console window?

I have an f# script using Windows Forms and I want it to run without showing console window (fsi.exe). Is it possible and how? Below is a script example. Console window screenshot

#r "mscorlib.dll"
#r "System.dll"

open System.IO
open System.Windows.Forms

let DoUsefulThing() = ()

let form = new Form()
let button = new Button(Text = "Do useful thing")
button.Click.AddHandler(fun _ _ -> DoUsefulThing())

form.Controls.Add(button)
form.ShowDialog()
like image 277
Alfa07 Avatar asked Dec 06 '22 15:12

Alfa07


1 Answers

as one option you can PInvoke and hide console window

#r "mscorlib.dll"
#r "System.dll"

open System.IO
open System.Windows.Forms

module FSI =
    [<System.Runtime.InteropServices.DllImport("user32.dll")>]
    extern bool ShowWindow(nativeint hWnd, int flags)
    let HideConsole() = 
        let proc = System.Diagnostics.Process.GetCurrentProcess()
        ShowWindow(proc.MainWindowHandle, 0)

FSI.HideConsole()

let DoUsefulThing() = ()


let form = new Form()
let button = new Button(Text = "Do useful thing")
button.Click.AddHandler(fun _ _ -> DoUsefulThing())

form.Controls.Add(button)
form.ShowDialog()
like image 119
desco Avatar answered May 16 '23 08:05

desco