F#学习:异步工作流初试

参考老赵文章里的代码:

http://blog.zhaojie.me/2010/04/fsharp-for-asp-net-2-implement-event-based-asynchronous-pattern-with-fsharp.html

写了一个简单的代码,异步的在两个流之间搬运数据。这里一个是打开的文件流,另一个是 Console 输出流:

open System.IO

let rec transferAsync (inStream: Stream) (outStream: Stream) buffer =
    async {
        let! lengthRead = inStream.AsyncRead(buffer, 0, buffer.Length)
        if lengthRead > 0 then
            do! outStream.AsyncWrite(buffer, 0, lengthRead)
            do! transferAsync inStream outStream buffer
    
    }

let buf = Array.zeroCreate 1024
let fs = File.OpenRead("E:\SQL游标代码模板.sql")
let outs = System.Console.OpenStandardOutput()

transferAsync fs outs buf 
    |> Async.RunSynchronously

printfn "complete."
System.Console.ReadLine() |> ignore

over.

posted on 2010-07-29 00:54  NeilChen  阅读(528)  评论(0编辑  收藏  举报

导航