Computer Science Experimentation

Thursday, May 5, 2011

F# interactive - how to hide/show the console

The F# interactive allows great object visibility and interactive capabilities even in running applications like server processes. So, it is a good idea to keep the F# interactive console available even in production versions of the code. But, it is not acceptable to leave several consoles open in the computer screen. The idea of this document is to show that consoles can be hidden and be shown as needed.
The following code shows how to manage application (start) and hide/show the F# interactive console applications.

//module ApplMgm

open System
open System.Diagnostics
open System.Collections.Generic
open System.Runtime.InteropServices;

System.Console.Title<-"AppMgm"

[< dllimport("user32.dll")>]
extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[<dllimport("user32.dll")>]
extern bool ShowWindow(IntPtr hWnd, int nCmdShow)

let fsharpPath= @"C:\Program Files (x86)\Microsoft F#\v4.0\fsi.exe"

type Process={Path:string;ExecName:string;ConsoleName:string}

type AppMgm()=
    let mutable applications= new Dictionary()
    let fsharpPath= @"C:\Program Files (x86)\Microsoft F#\v4.0\fsi.exe"

    member this.Apps with get() = applications and set a = applications <- a
    member this.fsharpFullPath= @"C:\Program Files (x86)\Microsoft F#\v4.0\fsi.exe"
    member this.start(keyName:string) =
        let procPath=applications.[keyName].Path
        let procName=applications.[keyName].ExecName
        Process.Start(fsharpPath,@"--readline+ --load:"+"\""+procPath+procName+"\"") |>ignore
        ()
    member this.hide(keyName:string) =
        let consName=applications.[keyName].ConsoleName
        let hWnd = FindWindow(null, consName) 
        if (hWnd <> 0n) then
            let b1=ShowWindow(hWnd, 0); // 0 = SW_HIDE
            ()        
    member this.show(keyName:string) =
        let consName=applications.[keyName].ConsoleName
        let hWnd = FindWindow(null, consName) 
        if (hWnd <> 0n) then
            let b1=ShowWindow(hWnd, 1); // 1 = SW_SHOWNORMA
            ()        
//--------------------------------------------------------------------------------------------------
//Configuration
let A= new AppMgm()
let d= new Dictionary()
let projPath= @"C:\Users\caxelrud\Documents\Visual Studio 2010\Projects\Utilities\"

d.Add("ap1",{Path=projPath;ExecName="app_1.fsx";ConsoleName="Application1"})
d.Add("ap2",{Path=projPath;ExecName="app_2.fsx";ConsoleName="Application2"})
//d.["ap2"]
A.Apps<-d
//A.Apps

A.start("ap1")
A.hide("ap1")
A.show("ap1")

No comments:

Post a Comment