Ok, I had time to write it, see below.

As with that C# library, the real strength comes from the compositionality. The same is true of F# async; it is easy to compose async code and write it very much like corresponding straight-line code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#light

open System

// define an async version of File.Exists
let AsyncFileExists file = 
    let pred = new Predicate<_>(System.IO.File.Exists)
    Async.BuildPrimitive(file, pred.BeginInvoke, pred.EndInvoke)

let Example() =
    let file = @"C:\foo.txt"
    try
        let exists = Async.Run(AsyncFileExists file, timeout = 1000)
        if exists then
            printfn "'%s' does exist" file
        else
            printfn "'%s' does not exist" file
    with 
        | :? TimeoutException -> printfn "timeout occurred"
            
Example()

This video has a nice example of async towards the end.

[link:channel9.msdn.com]

By on 12/17/2008 8:41 AM ()

THANKS

This is what I was looking for.

great.

Roberto

By on 12/17/2008 9:00 AM ()

Any time you have a Begin/End method pair, you can turn it into an Async with Async.BuildPrimitive:

[link:research.microsoft.com]

So I think here you'd just call BuildPrimitive to create the Async<bool>, and then use Async.Run with the timeout parameter. I'm not able to code it up right now, but hopefully those are the leads you need. (It will undoubtedly be short than that C# code.)

By on 12/17/2008 8:17 AM ()

I was expecting to see somethiing like

1
2
let csv = reader.readToEnd() // sync
let! csv = reader.AsyncReadToEnd() // async

Is there anyone out there who can code an File.Exist async sample?

Thanks anyway

By on 12/17/2008 8:38 AM ()

In the example I just posted, you could use AsyncFileExists with a "let!" in an async{} block the same way you'd use File.Exists with a "let". That's the compositionality I was referring to. Your example just had a single call, so there was no need to write an async block. But if it makes you happy,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#light

open System

// define an async version of File.Exists
let AsyncFileExists file = 
    let pred = new Predicate<_>(System.IO.File.Exists)
    Async.BuildPrimitive(file, pred.BeginInvoke, pred.EndInvoke)

let Example() =
    let file = @"C:\foo.txt"
    try
        let code = async {
            let! exists = AsyncFileExists file
            if exists then
                printfn "'%s' does exist" file
            else
                printfn "'%s' does not exist" file
        }
        Async.Run(code, timeout = 1000)
    with 
        | :? TimeoutException -> printfn "timeout occurred"
            
Example()

Note that this version runs the 'printf' under the timeout as well, but I did it just to show 'more code in the async block'.

By on 12/17/2008 8:48 AM ()

Hi Brian,

Does this mean that the thread calling Let exists = Async.Run(…) is blocked until File.Exists returns or until the timeout?

By on 12/17/2008 9:27 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper