Overview : async awit, async void 를 구현해 본다.
1.async awit (net.core)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public async Task ProcessWriteAsync() { string filePath = "temp.txt"; string text = $"Hello World{Environment.NewLine}"; await WriteTextAsync(filePath, text); } async Task WriteTextAsync(string filePath, string text) { byte[] encodedText = Encoding.Unicode.GetBytes(text); using var sourceStream = new FileStream( filePath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true); await sourceStream.WriteAsync(encodedText, 0, encodedText.Length); } |
2.async void (.net)
기다리지 않고 파일 쓰기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
SimpleWriteAsync(sPath, FileName, sText) ////////////////////////////////////////////////////////////////////////////////// public static void SimpleWriteAsync(string sPath, string FileName, string sText) { string PathFileName = sPath + @"\" + FileName; Task k = WriteTextAsync(PathFileName, sText); //비동기 호출 } public static async Task WriteTextAsync(string PathFileName, string sText) { using (FileStream fs = new FileStream(PathFileName, FileMode.Create)) { using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8)) { await w.WriteAsync(sText); } } } |