11 people like it.

Easy Wrapper for thread pool work

An easy wrapper for the TPL that works nicely with (|>)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
        open System.IO

        // Easy Wrapper for thread pool work
        let  sendToTPL task = 
              Async.StartAsTask <| async { return task }

        // Just add:      
        //       |>  sendToTPL  
        // to the end of a Let expression that can be run in the background 
        // while other work is performed

        // 
        // Simple example of loading three large text data files
        // 
        let AlphaFileName = "c:\somewhere"
        let BetaFileName  = "c:\somewhere"
        let DeltaFileName = "c:\somewhere"


        let placeFileIntoArray = File.ReadAllLines
        
        // Load/Parse files
      
        let FileAlphaLines = AlphaFileName    
                                     |> placeFileIntoArray 
                                     |> sendToTPL
        let FileBetaLines  = BetaFileName          
                                     |> placeFileIntoArray 
                                     |> sendToTPL  
        let FileDeltaLines = DeltaFileName        
                                     |> placeFileIntoArray 
                                     |> sendToTPL
         
        // Do other work here
        // ...

        // Creates header -> column index Map using given delimiter
        let CreateHeadersMap fileslines delimiter =
             // not important for this example
             0 // added so example compiles

        // Later use .Result to get the work results when you need them
        
        let FileAlphaHeaderMap  = CreateHeadersMap FileAlphaLines.Result [|'\t'|]
        let FileBetaHeadersMap  = CreateHeadersMap FileBetaLines.Result  [|','|]
        let FileDeltaHeadersMap = CreateHeadersMap FileDeltaLines.Result [|'\t'|] 
namespace System
namespace System.IO
val sendToTPL : task:'a -> System.Threading.Tasks.Task<'a>

Full name: Script.sendToTPL
val task : 'a
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<'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.StartAsTask : computation:Async<'T> * ?taskCreationOptions:System.Threading.Tasks.TaskCreationOptions * ?cancellationToken:System.Threading.CancellationToken -> System.Threading.Tasks.Task<'T>
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val AlphaFileName : string

Full name: Script.AlphaFileName
val BetaFileName : string

Full name: Script.BetaFileName
val DeltaFileName : string

Full name: Script.DeltaFileName
val placeFileIntoArray : arg00:string -> string []

Full name: Script.placeFileIntoArray
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 3 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  static member Encrypt : path:string -> unit
  static member Exists : path:string -> bool
  ...

Full name: System.IO.File
File.ReadAllLines(path: string) : string []
File.ReadAllLines(path: string, encoding: System.Text.Encoding) : string []
val FileAlphaLines : System.Threading.Tasks.Task<string []>

Full name: Script.FileAlphaLines
val FileBetaLines : System.Threading.Tasks.Task<string []>

Full name: Script.FileBetaLines
val FileDeltaLines : System.Threading.Tasks.Task<string []>

Full name: Script.FileDeltaLines
val CreateHeadersMap : fileslines:'a -> delimiter:'b -> int

Full name: Script.CreateHeadersMap
val fileslines : 'a
val delimiter : 'b
val FileAlphaHeaderMap : int

Full name: Script.FileAlphaHeaderMap
property System.Threading.Tasks.Task.Result: string []
val FileBetaHeadersMap : int

Full name: Script.FileBetaHeadersMap
val FileDeltaHeadersMap : int

Full name: Script.FileDeltaHeadersMap

More information

Link:http://fssnip.net/1P
Posted:13 years ago
Author:Paul Greene
Tags: async