Putting a block of code inside async{} does not magically make it cancellable, there needs to be some Async primitive inside that enables some kind of cooperative cancellation. In this instance, you could do

1
2
3
4
5
6
7
8
9
10
11
let AsyncConnect (c : TcpClient, ipAddr:IPAddress, port:int) = 
    Async.FromBeginEnd((fun (cb,o)->c.BeginConnect(ipAddr,port,cb,o)), c.EndConnect)
let bar (ip, port) =
  async
    { 
      let c = new TcpClient()
      printfn "connecting to: %s:%d" ip port
      //c.Connect(IPAddress.Parse(ip), port)
      do! AsyncConnect(c, IPAddress.Parse(ip), port)
      c.Close()
    }

which uses BeginConnect/EndConnect inside the Async, and thus becomes cancellable via the timeout.

By on 8/3/2009 4:35 PM ()

(as usual:) many thanks !!!

By on 8/3/2009 5:05 PM ()
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