Dave
Dave

Reputation: 53

Very simple F# form locks up on keyboard input

Using the ultra-simple code below, as soon as I press a key on my keyboard while the form is in focus, the form completely locks up. I'm running this inside of F# interactive. The only way to close the form is by clicking "Reset Session" in F# interactive. I've tried adding event handlers to KeyPress, with the same results. I've had no problem adding mouse event handlers, menus, combo boxes etc.

I must be doing something wrong, as something as obvious as pressing a key on a keyboard probably shouldn't be a bug at this point for F#. Any ideas?

// Add reference to System.Windows.Forms to project
open System.Windows.Forms

let a = new Form()
a.Visible <- true

I'm using F# 2.0 for Windows + Visual Studio 2008 (April 2010 release) on Windows XP.

Thanks!

Upvotes: 5

Views: 498

Answers (3)

J D
J D

Reputation: 48687

This thread-related bug has been in F# for many years and was a massive PITA for us when our visualization products were built upon Windows Forms. You might be able to get responsiveness back by hitting return a few times in your F# interactive session.

The only advice I can give is to use WPF instead but getting that working reliably from F# interactive is still quite tricky. This is probably the single most useful aspect of our F# for Visualization library...

Upvotes: 0

Brian
Brian

Reputation: 118895

I think you need a call to

Application.Run(a)

but I don't have time to try and verify right now.

EDIT:

A useful thing to do is: create a C# Windows Form project, and see what code it starts you off with. It gives you this:

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

so of course you can do the same in F#:

open System.Windows.Forms 

let Main() =
    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(false)
    Application.Run(new Form())

[<System.STAThread>]
do
    Main()

Upvotes: 3

Tomas Petricek
Tomas Petricek

Reputation: 243096

You definitely don't need to call Application.Run(a) in F# Interactive, because it manages it's own message loop (in fact, you cannot do that).

Creating a form and setting Visible to true should work (and it does work on my machine!) Unfortunatelly, I'm not sure what could cause the problem that you reported, but it is definitely not the expected behavior (it seems to be some bug).

Using ShowDialog is not a good idea, because when you call it, it blocks you from entering further commands in F# Interactive until the dialog closes. This is very unfortunate - typical usage of F# Interactive is to create and show a form and then modify it by entering other commands. For example:

> let a = new Form();;
val a : Form = System.Windows.Forms.Form, Text: 

> a.Visible <- true;;  // Displays the form 
val it : unit = ()

> a.Text <- "Hello";;  // Changes title of the form
val it : unit = ()

(Marked as community wiki, because I didn't really answer the question.)

Upvotes: 2

Related Questions