【发布时间】:2011-06-19 05:49:06
【问题描述】:
我从第 13 章的 Don Syme 的 Expert F# 中获得了这个代码示例
open System.ComponentModel
open System.Windows.Forms
let worker = new BackgroundWorker()
let numIterations = 1000
worker.DoWork.Add(fun args ->
let rec computeFibonacci resPrevPrev resPrev i =
//compute next result
let res = resPrevPrev + resPrev
//at the end of the computation and write the result into the mutable state
if i = numIterations then
args.Result <- box res
else
//compute the next result
computeFibonacci resPrev res (i+1)
computeFibonacci 1 1 2)
worker.RunWorkerCompleted.Add(fun args ->
MessageBox.Show(sprintf "result = %A" args.Result) |> ignore)
//execute the worker
worker.RunWorkerAsync()
我知道这段代码计算斐波那契数,但我的问题是,有什么方法可以让我创建计算斐波那契的函数,然后将其包装在后台工作代码中?有例子会更好:)谢谢
【问题讨论】:
标签: .net multithreading f# backgroundworker