Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6048455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:27:54+00:00 2026-05-23T07:27:54+00:00

I get this code example from Expert F# by Don Syme from chapter 13

  • 0

I get this code example from Expert F# by Don Syme from chapter 13

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()

i knew that this code compute fibonacci number, but my question is, is there any way that i make the function for compute fibonacci and then wrap it in the background worker code? with example would be better 🙂 thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:27:54+00:00Added an answer on May 23, 2026 at 7:27 am

    Just to slightly simplify the solution by Ankur – you don’t need to use lazy values in this case. The simplest way is to declare the computeFibonacci function first and then call it from the DoWork event.

    The important point that you need to change computeFibonacci to return the result instead of storing it to args because args are only available when called from DoWork:

    let numIterations = 1000
    
    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 
          // Return result from the function
          res
      else 
          //compute the next result
          computeFibonacci resPrev res (i+1)
    

    Then you can create background worker that calls compueFibonacci and storest the result in args:

    let worker = new BackgroundWorker()
    
    worker.DoWork.Add(fun args ->
        // Call fibonacci and store the result in `Result`
        // (the F# compiler converts number to `obj` automatically)
        args.Result <- computeFibonacci 1 1 2)
    
    worker.RunWorkerCompleted.Add(fun args ->
        MessageBox.Show(sprintf "result = %A" args.Result) |> ignore)
    
    worker.RunWorkerAsync()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hI, I'm trying to get this code from Larry Nyhoff's book to compile in
I get an error when I compile this code: using System; public struct Vector2
I've been trying to get this code to work for hours! All I need
I'm trying to get this piece of code working a little better. I suspect
I have this code to connect to Active Directory and get all the groups
I'm using this code, and I get the stack trace that is listed below.
This code function LoadContent(Id) { alert('Controls/Network/NetworkDetail.aspx?' + rnd() + '&CtlId=' + Id); $.get('Controls/Network/NetworkDetail.aspx?' +
I am completely stumped as to why this code does not get any SDL
When I try this code: class MyStuff: def average(a, b, c): # Get the
I need to get the last part of current directory, for example from /Users/smcho/filegen_from_directory/AIRPassthrough

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.