Computer Science Experimentation

Monday, May 23, 2011

Data and Historian Service using WCF and F# interactive - rev 2

This blog presents the revision 2 from the material from the blog:
WCF with F# interactive – Data Server example - Dec/25/2010
http://caxelrud.blogspot.com/2010/12/wcf-with-f-interactive-data-server.html
These are the improvements:
- The data value is of type Object. The examples show how to work with basic types as float, integer, strings and other types as dateTime. It can also use array of the basic types.
- The data server also stores status and time-stamp.
- The data server optionally can operate as a historian using an in-memory circular buffer storing values (objects), status and time-stamps.

Check the code and examples at:
http://cid-bdc87ef39b001785.office.live.com/view.aspx/Fsharp/DataServer%5E_doc%5E_1%5E_0.docx

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")