Hi Daniel,

The behaviour does seem a bit strange, calling form.Invoke should be enough to get you back to the GUI thread. The only thing I can think of is that your snippet does not show were "form" created, if this was created on another thread then that could be the problem.

However you should really need to interact with the Application object from within fsi, that should all be taken care of by fsi in the background. You should be calling form.Show() to launch forms. You should really need a timer to call Application.DoEvents as events are process by the GUI thread when ever it is "idle" - its only if you are doing something computationally intensive on the GUI thread that you should need to call it.

The easiest way to resolve this is probably to use a System.Windows.Forms.Timer instread. This timer is guaranteed to be invoked on the GUI thread so gets rid of these sorts of problems.

Cheers,
Rob

By on 12/6/2007 10:31 PM ()

Hi Robert,

Thanks for the swift reply. Congrats on the book - yours and Don's are on their way to me at the mo. Looking forward to Jon's too.

I amended the SimpleForm demo with your recommendation so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#light

open System
open System.IO
open System.Windows.Forms
open Printf

// Create a form and set some properties

let form = new Form()

form.Text <- "My First F# Form"
form.Visible <- true

let menu = form.Menu <- new MainMenu()
let mnuFile = form.Menu.MenuItems.Add("&File")

let mnuiOpen = 
  new MenuItem("&Open...", 
               new EventHandler(fun _ _ -> 
                   let d = new OpenFileDialog() in 
                   d.InitialDirectory <- "c:\\";
                   d.Filter <- "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                   d.FilterIndex <- 2;
                   d.RestoreDirectory <- true;
                   if d.ShowDialog() = DialogResult.OK then 
                       match d.OpenFile() with 
                       | null -> printf "Ooops... Could not read the file...\n"
                       | s -> 
                           let r = new StreamReader(s) in 
                           printf "The first line of the file is: %s!\n" (r.ReadLine());
                           s.Close();
               ), 
               Shortcut.CtrlO)

mnuFile.MenuItems.Add(mnuiOpen)

// Added Forms.Timer and form.Show()
let timer = new Windows.Forms.Timer(Interval = 1000)
timer.Tick.Add( fun _ -> Application.DoEvents() ; print_endline <| "tick: " + DateTime.Now.ToString(); )
timer.Start()
form.Show()

Running VsFsi (making sure to #q;; if already loaded) I executed every line with Alt-Enter. The form is created and shown and the VsFsi window shows this:

> tick: 07/12/2007 15:29:07
tick: 07/12/2007 15:29:08
tick: 07/12/2007 15:29:09
tick: 07/12/2007 15:29:10
;;
> tick: 07/12/2007 15:29:27
tick: 07/12/2007 15:29:27
tick: 07/12/2007 15:29:28
tick: 07/12/2007 15:29:29
tick: 07/12/2007 15:29:30
tick: 07/12/2007 15:29:31
tick: 07/12/2007 15:29:32
tick: 07/12/2007 15:29:33

At the 4th second I place my mouse over the newly created form and see the hour-glass. At the same time the timer stops ticking. Neither the form or the timer are working now. Not until I enter ;; into VsFsi whereupon the tick starts up again. (You can see a pause of 17 sec between placing the cursor over the form and typing ;; into VsFsi.

I then navigate to File -> Open, then press Cancel. The dialog stays open and hangs, and the tick stops again. Typing ;; into VsFsi once again free up the dialog and start the tick again. Now I am able to File -> Open -> Cancel without hanging the message loop or stopping the tick.

Any further ideas here would be most welcome.

Warm regards,

Daniel

By on 12/7/2007 7:44 AM ()

Hi,

I would be very grateful if anyone could verify whether or not this behavior is expressed when running the SimpleForm demo from within Visual Studio. This could help me figure out whether this problem is to do with my system configuration or whether this is a problem with the distribution. Without a solution I'm finding it difficult to demonstrate F# as an interactive visualization platform and potential replacement for Matlab / R.

Many thanks for any assistance in this,

Daniel

By on 12/11/2007 4:24 AM ()

Hi Daniel,

Sorry for the slow response, I missed your orginal reply. I've played arround with the listing you provided and was experincing the same problem as you, with or without the timer, which shouldn't be necessary. The problem seems to come from setting the "form.Visible <- true" to show the form then dynamically adding the menu. I don't why this should be the case but the following listing seems to work without problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#light
open System
open System.IO
open System.Windows.Forms
open Printf

// Create a form and set some properties
let form = new Form()

form.Text <- "My First F# Form"

let menu = form.Menu <- new MainMenu()
let mnuFile = form.Menu.MenuItems.Add("&File")

let mnuiOpen = 
  new MenuItem("&Open...", 
               new EventHandler(fun _ _ -> 
                   let d = new OpenFileDialog() in 
                   d.InitialDirectory <- "c:\\";
                   d.Filter <- "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                   d.FilterIndex <- 2;
                   d.RestoreDirectory <- true;
                   if d.ShowDialog() = DialogResult.OK then 
                       match d.OpenFile() with 
                       | null -> printf "Ooops... Could not read the file...\n"
                       | s -> 
                           let r = new StreamReader(s) in 
                           printf "The first line of the file is: %s!\n" (r.ReadLine());
                           s.Close();
               ), 
               Shortcut.CtrlO)

mnuFile.MenuItems.Add(mnuiOpen)
form.Show()

Strangely I can dynamically add controls to the form once shown without problem:

1
2
form.Controls.Add(new TextBox())
form.Controls.Add(new TextBox(Top=50))

Cheers,
Rob

By on 12/11/2007 6:27 AM ()

Hi Robert,

I really appreciate the response. Sorry to ask again, but could you also verify that the GUI hangs once again when clicking Cancel in the File Open Dialog.

I guess I'll have to live without the dynamic visualization part of the F# story for the moment - there's still plenty to tell. I'll email fsbugs and keep this thread up to date with any further findings.

Warm regards,

Daniel

By on 12/11/2007 8:09 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper