0

I have the following code in a C# lib

public class A
{
  public async Task CreateEntity(Model m) { 
    /* no return here but await for db insert and set m.Id */ 
  }
}

I have another application in F# that references this lib and try to call CreateEntity. I would like to be able to do await on it but I can not find any examples of this . How to block or 'await' in my F# app? It seems that there is an issue with the fact that the return value is not Task<T>

UPDATE

Could someone make this one work correctly

I have been able to reproduce the issue with the following snippet

open System.Threading.Tasks

let awaitTask = Async.AwaitIAsyncResult >> Async.Ignore

printfn "Before"
awaitTask(Task.Delay 10000)
printfn "After"

It returns in less than a sec, so it does not await

I have indeed seen that this question is related to another post. But as none of the answers seem to work, I have asked it again. If someone could provide a complete F# snippet that work that would definitely help me a lot.

Dave
  • 1,835
  • 4
  • 26
  • 44
  • Try looking at [this SO question](http://stackoverflow.com/questions/8022909/how-to-async-awaittask-on-plain-task-not-taskt) – Icemanind Oct 15 '14 at 17:16
  • I have seen this but it does not seem to work at all . I have tries all the solutions there and none succeed. It seems to be related to the fact that I return a plain Task. – Dave Oct 15 '14 at 17:20
  • I think there is a similar question with solution http://stackoverflow.com/questions/8022909/how-to-async-awaittask-on-plain-task-not-taskt – cezarypiatek Oct 15 '14 at 17:26
  • yeah it suck - you have to get the handle from task (need to cast it before as it) and then AwaitWaitHandle on it - but there was a question on this just a few days before ... – Random Dev Oct 15 '14 at 17:26
  • @Uriel: same reference as icemanind but have not been able to make it work. – Dave Oct 15 '14 at 17:27
  • use this: [Async.AwaitWaitHandle](http://msdn.microsoft.com/en-us/library/vstudio/ee370283(v=vs.100).aspx) with this: [Task.IAsyncResult.Async.WaitHandle](http://msdn.microsoft.com/en-us/library/dd235637(v=vs.110).aspx) and as I said you have to cast the `Task` into `IAsyncResult` first – Random Dev Oct 15 '14 at 17:28
  • @Dave have you seen my last comment - this should work `(task :> IAsyncResult).WaitHandle |> Async.AwaitWaitHandle` (modulo misspellings - don't have any fsharp around atm) – Random Dev Oct 15 '14 at 17:33
  • @Dave btw: your snippet will of course not delay - you did not start your async-workflow ^^ (Asycn.RunSynchronously) – Random Dev Oct 15 '14 at 17:35
  • I've tried x.CreateEntity(m) |> Async.AwaitIAsyncResult |> ignore and it works – cezarypiatek Oct 15 '14 at 17:37
  • @CarstenKönig: too much information at the same time:) I was actually reading WaitHandle stuff. Do I need to RunSynchronously my task ? (learning F# from today so I am bit slow :) ) – Dave Oct 15 '14 at 17:38
  • @Dave yes - Async-Workflows in F# are only started when you do something like Async.Start, Async.RunSync., ... IMO that makes them far more usefull than their Task counterparts - you have more control – Random Dev Oct 15 '14 at 17:40
  • @Uriel - the Task will get created/started yes but that has nothing to do with the AsyncWorkflow waiting on it - indeed you only declared the computation and happened to force the sideeffect from the task but you surely will not wait – Random Dev Oct 15 '14 at 17:42
  • @CarstenKönig: Could you show me a snippet of the following 'Before/delay 10s/After' application that i wrote above. I don't get how all this is going to work – Dave Oct 15 '14 at 17:53
  • @Carsten König - I've made additional test and you're right. I've also tried x.CreateEntity(m).Wait() (in F# ofc) and it really started to waiting. What's wrong with that solution?? – cezarypiatek Oct 15 '14 at 18:04
  • This question was already asked: http://stackoverflow.com/questions/25166363/async-await-not-catching-task-exception/25171861#25171861 – desco Oct 15 '14 at 18:27
  • I am sorry but I can't make it work . It is probably my fault . Could someone provide a fully F# working snippet of my updated example ? – Dave Oct 16 '14 at 08:17
  • I'm not sure, but you could also be thinking on "how to do async await in F#", or how to translate (mechanically or as a concept) such code to F#. These could be of help [Translating async-await C# code to F# with respect to the scheduler](http://stackoverflow.com/questions/24813359/translating-async-await-c-sharp-code-to-f-with-respect-to-the-scheduler) and [How to use FSharpx TaskBuilder with functions taking parameters](http://stackoverflow.com/questions/24981193/how-to-use-fsharpx-taskbuilder-with-functions-taking-parameters). I think ``TaskBuilder`` in FSharpx isn't 1:1 with C# await. – Veksi Oct 25 '14 at 19:17

0 Answers0