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

  • Home
  • SEARCH
  • 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 4325100
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:13:19+00:00 2026-05-21T09:13:19+00:00

I have read few F# tutorials and I have noticed how easy is to

  • 0

I have read few F# tutorials and I have noticed how easy is to perform asynchronous and parallel programming in F# compared to C#. Thus, I am trying to write an F# library which will be called from C# and take take a C# function (delegate) as a parameter and run it asynchronously.

I have managed to pass the function so far (I am even able to cancel) but what I miss is how to implement a callback back to C# which will be executed it as soon as the asynchronous operation is completed. (e.g. the function AsynchronousTaskCompleted?). Also I was wondering if I can post (e.g. Progress %) back to F# from then function AsynchronousTask.

Can someone please help me?

This is the code I have written so far (I am not familiar with F# so the following code may be wrong or poorly implemented).

//C# Code Implementation (How I make the calls/handling)
        //Action definition is: public delegate void Action();
        Action action = new Action(AsynchronousTask);
        Action cancelAction = new Action(AsynchronousTaskCancelled);
        myAsyncUtility.StartTask2(action, cancelAction);
        Debug.WriteLine("0. The task is in progress and current thread is not blocked");
        ......
        private void AsynchronousTask()
        {
            //Perform a time-consuming task
            Debug.WriteLine("1. Asynchronous task has started.");
            System.Threading.Thread.Sleep(7000);
            //Post progress back to F# progress window?
            System.Threading.Thread.Sleep(2000);
        }        
        private void AsynchronousTaskCompleted(IAsyncResult asyncResult)
        {           
            Debug.WriteLine("2. The Asynchronous task has been completed - Event Raised");
        }
        private void AsynchronousTaskCancelled()
        {
            Debug.WriteLine("3. The Asynchronous task has been cancelled - Event Raised");
        }

//F# Code Implementation
  member x.StartTask2(action:Action, cancelAction:Action) = 
        async {
            do! Async.FromBeginEnd(action.BeginInvoke, action.EndInvoke, cancelAction.Invoke)
            }|> Async.StartImmediate
        do printfn "This code should run before the asynchronous operation is completed"    
        let progressWindow = new TaskProgressWindow()
        progressWindow.Run() //This class(type in F#) shows a dialog with a cancel button
        //When the cancel button is pressed I call Async.CancelDefaultToken()

  member x.Cancel() =
        Async.CancelDefaultToken()
  • 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-21T09:13:19+00:00Added an answer on May 21, 2026 at 9:13 am

    To get the benefits of F# asynchronous workflow, you have to actually write the asynchronous computation in F#. The code you’re trying to write will not work (i.e. it may run, but won’t be useful).

    When you’re writing asynchronous computations in F#, you can make asynchronous calls using let! and do!. This allows you to use other primitive non-blocking computations. For example, you can use Async.Sleep instead of Thread.Sleep.

    // This is a synchronous call that will block thread for 1 sec
    async { do Thread.Sleep(1000) 
            someMoreStuff() }
    
    // This is an asynchronous call that will not block threads - it will create 
    // a timer and when the timer elapses, it will call 'someMoreStuff' 
    async { do! Async.Sleep(1000)
            someMoreStuff() }
    

    You can only use asynchronous operations inside the async block and it relies on the way how F# compiler handles do! and let!. There is no (easy) way to get real non-blocking execution for code that is written in sequential way (e.g. in C# or outside of async block in F#).

    If you want to use F# to get the benefits of asynchronous workflows, then the best option is to implement the operations in F# and then expose them to C# using Async.StartAsTask (which gives you Task<T> that C# can easily use). Something like this:

    let someFunction(n) = async {
      do! Async.Sleep(n)
      Console.WriteLine("working")
      do! Async.Sleep(n)
      Console.WriteLine("done") 
      return 10 }
    
    type AsyncStuff() = 
      member x.Foo(n) = someFunction(n) |> Async.StartAsTask
    
    // In C#, you can write:
    var as = new AsyncStuff()
    as.Foo(1000).ContinueWith(op =>
        // 'Value' will throw if there was an exception
        Console.WriteLine(op.Value))
    

    If you don’t want to use F# (at least for the implementation of your async computations), then asynchronous workflows won’t help you. You can implement similar thing using Task, BackgroundWorker or other C# technologies (but you’ll loose the ability to run operations easily without blocking threads).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.