Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How run in background a blocking call in F#?

I need to call in the background a API that call a webservice. I don't wish to turn the (very complex) method to async, just say "do all this on the background".

But I'm lost in how do this with F#. This is what I have:

            let task = async {
                let result = SyncApi.syncData(login.url, login.zone, login.user, login.pwd) <-- THIS MUST RUN IN BACKGROUND...
                match result with
                |Some(msg) -> failwith msg
                | None -> ()
            }

            task
            |> Async.Catch
            |> Async.RunSynchronously
            |> fun x -> 
                  match x with
                  | Choice1Of2 x -> rootPage.Navigation.PopToRootAsync(true) |> ignore
                  | Choice2Of2 ex -> showMsgError(ex.Message)
like image 875
mamcx Avatar asked Nov 10 '17 23:11

mamcx


People also ask

Should you run commands in the foreground or the background?

Sometimes, however, running commands in the foreground can present a set of challenges. The command can take too long to exit causing you to waste precious time and other times, it can be totally attached to the shell session leaving you stuck. In such cases, running a command in the background is your best bet.

How do I bring a background process to the foreground?

To bring a background process to the foreground, use the fg command: If you have multiple background jobs, include % and the job ID after the command: To terminate the background process, use the kill command followed by the process ID: To move a running foreground process in the background:

How to unblock the background of the application?

The unblocking background so that you can change background is easy what you have to do you have to delete string value which in same path where you have made it. After deleting same restart your system and you are able to see that personalized options are unlocked.

What is running the command in the foreground process called?

This is called running the command in the foreground or foreground process. When a process runs in the foreground, it occupies your shell, and you can interact with it using the input devices. What if the command takes a long time to finish, and you want to run other commands in the meantime? You have several options at your disposal.


1 Answers

If you're looking for simple fire and forget style to start the API call an don't use the result on the current thread, Async.Start(task) might be what you're looking for. It takes the Async workflow, starts it on a thread pool and immediately returns so that your method can continue.

But seeing that you need the result to either change navigation or show an error message, you might need to call the SyncApi synchronously on the current thread and wait for its result.

Alternatively, if your application model allows it, you can do something like this:

(* Define the task including error handling. *)
let task = async {
    let result = SyncApi.syncData(login.url, login.zone, login.user, login.pwd)
    match result with
    | Some msg ->
        (* This may have to be posted back to the UI context.
           Correct way depends on technology (Xamarin vs. WPF vs. MVC...) *)
        showMsgError msg
    | None -> ()
}

(* Fire and forget the async API call. *)
Async.Start(task)

(* Optimistically navigate away immediately,
   while `task` may still be in progress. *)
rootPage.Navigation.PopToRootAsync(true) |> ignore

This will start the task on a thread pool, navigate away, but in case the async task failed, it will trigger the error message. However it assumes that your application can show the error message asynchronously for example as a popup, not only in the context of the page that started the task.

like image 135
Honza Brestan Avatar answered Oct 01 '22 00:10

Honza Brestan