F #: window shape in compiled program

To visualize data from the F # interactive console, I can do the following:

open System.Windows.Forms
let  testgrid (x) =    
          let form = new Form(Visible = true)    
          let data = new DataGridView(Dock = DockStyle.Fill)    
          form.Controls.Add(data)    
          data.DataSource <- x

testgrid [|(1,1);(2,2)|]

But if I put the above into a compiled F # program and call testgrid [|(1,1);(2,2)|] inside the program, I get only a frozen window without data. what needs to be done to make this testgridwork for an F # executable program? EDIT: with ildjarn answer and some search, is the following code OK? Any pitfalls?

let testgrid x =
    let makeForm() = 
        use form = new Form()
        new DataGridView(Dock = DockStyle.Fill, DataSource = x) |> form.Controls.Add
        Application.Run form
    let thread = new System.Threading.Thread(makeForm)
    thread.SetApartmentState(Threading.ApartmentState.STA)
    thread.Start()
+3
source share
1 answer

You need a message pump; FSI already has one, so your code works from the FSI console, but a stand-alone program will not have it if you don't:

open System
open System.Windows.Forms

let testgrid x =
    use form = new Form()
    new DataGridView(Dock = DockStyle.Fill, DataSource = x) |> form.Controls.Add
    Application.Run form

[<STAThread>]
do testgrid [|(1,1);(2,2)|]
+3
source

All Articles