Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val uri : System.Uri
namespace System
Multiple items type Uri = new : uriString:string -> Uri + 5 overloads member AbsolutePath : string member AbsoluteUri : string member Authority : string member DnsSafeHost : string member Equals : comparand:obj -> bool member Fragment : string member GetComponents : components:UriComponents * format:UriFormat -> string member GetHashCode : unit -> int member GetLeftPart : part:UriPartial -> string ...
Full name: System.Uri
-------------------- System.Uri(uriString: string) : unit System.Uri(uriString: string, uriKind: System.UriKind) : unit System.Uri(baseUri: System.Uri, relativeUri: string) : unit System.Uri(baseUri: System.Uri, relativeUri: System.Uri) : unit
val webClient : System.Net.WebClient
namespace System.Net
Multiple items type WebClient = inherit Component new : unit -> WebClient member BaseAddress : string with get, set member CachePolicy : RequestCachePolicy with get, set member CancelAsync : unit -> unit member Credentials : ICredentials with get, set member DownloadData : address:string -> byte[] + 1 overload member DownloadDataAsync : address:Uri -> unit + 1 overload member DownloadFile : address:string * fileName:string -> unit + 1 overload member DownloadFileAsync : address:Uri * fileName:string -> unit + 1 overload member DownloadString : address:string -> string + 1 overload ...
Full name: System.Net.WebClient
-------------------- System.Net.WebClient() : unit
val html : string
member System.Net.WebClient.AsyncDownloadString : address:System.Uri -> Async<string>
val getHtmlList : (string list -> string [])
Full name: index.getHtmlList
module Seq
from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>
Full name: Microsoft.FSharp.Collections.Seq.map
Multiple items type Async static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit) static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate) static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool> static member AwaitTask : task:Task -> Async<unit> static member AwaitTask : task:Task<'T> -> Async<'T> static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool> static member CancelDefaultToken : unit -> unit static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>> static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T> static member FromBeginEnd : arg:'Arg1 * beginAction:('Arg1 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T> static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * beginAction:('Arg1 * 'Arg2 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T> static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * arg3:'Arg3 * beginAction:('Arg1 * 'Arg2 * 'Arg3 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T> static member FromContinuations : callback:(('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T> static member Ignore : computation:Async<'T> -> Async<unit> static member OnCancel : interruption:(unit -> unit) -> Async<IDisposable> static member Parallel : computations:seq<Async<'T>> -> Async<'T []> static member RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:CancellationToken -> 'T static member Sleep : millisecondsDueTime:int -> Async<unit> static member Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit static member StartAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions * ?cancellationToken:CancellationToken -> Task<'T> static member StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>> static member StartChildAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions -> Async<Task<'T>> static member StartImmediate : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit static member StartWithContinuations : computation:Async<'T> * continuation:('T -> unit) * exceptionContinuation:(exn -> unit) * cancellationContinuation:(OperationCanceledException -> unit) * ?cancellationToken:CancellationToken -> unit static member SwitchToContext : syncContext:SynchronizationContext -> Async<unit> static member SwitchToNewThread : unit -> Async<unit> static member SwitchToThreadPool : unit -> Async<unit> static member TryCancelled : computation:Async<'T> * compensation:(OperationCanceledException -> unit) -> Async<'T> static member CancellationToken : Async<CancellationToken> static member DefaultCancellationToken : CancellationToken
Full name: Microsoft.FSharp.Control.Async
-------------------- type Async<'T>
Full name: Microsoft.FSharp.Control.Async<_>
static member Async.Parallel : computations:seq<Async<'T>> -> Async<'T []>
staticvoid Main(string[] args)
{
byte[] readBuffer;
var fs = File.OpenRead(@"c:\somefile.txt");
readBuffer =newbyte[fs.Length];
var result = fs.BeginRead(readBuffer, 0, (int)fs.Length,
OnReadComplete, fs);
//do other work here while file is read...
Console.ReadLine();
}
privatestaticvoid OnReadComplete(IAsyncResult result)
{
var stream = (FileStream)result.AsyncState;
var bytesRead = stream.EndRead(result);
Console.WriteLine("Read {0} bytes successfully.", bytesRead);
stream.Dispose();
}
APM
Asynchronous Programming Model
Separate XYBegin und XYComplete methods - readability
No way to cancel
No error handling
No using statement for resources possible - create and dispose in different methods
No synchronization between callback and caller thread
staticvoid Main(string[] args)
{
byte[] readBuffer;
var fs = File.OpenRead(@"c:\somefile.txt");
readBuffer =newbyte[fs.Length];
var result = fs.BeginRead(readBuffer, 0, (int)fs.Length,
asyncResult =>
{
var bytesRead = fs.EndRead(asyncResult);
Console.WriteLine("Read {0} bytes successfully.", bytesRead);
fs.Dispose();
}, null);
//do other work here while file is read...
Console.ReadLine();
}
privatestatic Task<int> ReadFileAsync(string filePath)
{
var fs = File.OpenRead(filePath);
var readBuffer =newbyte[fs.Length];
var readTask = fs.ReadAsync(readBuffer, 0, (int)fs.Length);
readTask.ContinueWith(task =>
{
if (task.Status == TaskStatus.RanToCompletion)
Console.WriteLine("Read {0} bytes from file {1}",
task.Result, filePath);
else
Console.WriteLine("Error with file {0}.", filePath);
fs.Dispose();
});
return readTask;
}
TAP - Task-based Async Programming
1: 2: 3: 4: 5: 6: 7: 8:
staticvoid Main(string[] args)
{
var read1= ReadFileAsync(@"c:\somefile.txt");
var read2= ReadFileAsync(@"c:\someotherfile.txt");
Task.WhenAll(read1, read2)
.ContinueWith(task => Console.WriteLine("All done."));
//do other work here while files are read...
}
leturlList= [
"http://www.microsoft.com""http://www.google.com""http://www.amazon.com" ]
letfetchHtmlAsyncurl=async {
leturi=newSystem.Uri(url)
letwebClient=newSystem.Net.WebClient()
let!html=webClient.AsyncDownloadString(uri)
returnhtml
}
letgetHtmlList=Seq.mapfetchHtmlAsync// Build an Async<'a> for each site>>Async.Parallel// each Async<'a> across different threads>>Async.RunSynchronously// Run each Async<'a>, non-blocking waitlethtmlList=urlList|>getHtmlList