下面是一个使用f的工作示例#
MailboxProcessor
将表单托管在单独的线程上。它使用文本框而不是图像,但这是相同的想法。
module ConsoleApp
open System
open System.Windows.Forms
type WinForm () as form =
inherit Form ()
let textBox = new TextBox()
do form.Controls.Add(textBox)
member __.SetText text = textBox.Text <- text
type Message = SetText of string
type UpdateText = delegate of unit -> unit
let form = new WinForm()
let agent =
MailboxProcessor.Start
<| fun inbox ->
let rec loop () =
async {
let! (SetText text) = inbox.Receive()
form.Invoke(UpdateText(fun () -> form.SetText(text))) |> ignore
return! loop ()
}
loop ()
[<EntryPoint>]
let main argv =
async { form.ShowDialog() |> ignore } |> Async.Start
let rec loop () =
printf "Enter Text: "
match Console.ReadLine() with
| "exit" -> ()
| text -> agent.Post <| SetText text; loop()
loop ()
0
注意,显示表单时
ShowDialog
,否则承载表单的线程将返回线程池,表单将变得无响应。