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)
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.
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:
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With