c# - Trying to use async method TextReader.ReadBlockAsync, but getting "the 'await' operator can only be used within an async method" -
this question has answer here:
i want call asynchronous reader method of textreader
, compile error:
var buffer = new char[10]; textreader reader = new streamreader(@"d:\temp\abc.txt"); // following statement compile error var readcount = await reader.readblockasync(buffer, 0, buffer.length);
this error:
the 'await' operator can used within async method. consider marking method 'async' modifier , changing return type 'task'.
what right way use readblockasync
?
you need mark method async
identifier mentioned others. method needs return task
, task<t>
or void
. void
returning async methods reserved async event handlers. in case you'd want return task<string>
public async task<string> readasync() { var buffer = new char[10]; textreader reader = new streamreader(@"d:\temp\abc.txt"); var readcount = await reader.readblockasync(buffer, 0, buffer.length); return new string(buffer.take(readcount).toarray()); }
you may find following resources useful: best practices async/await, asynchronous programming async-await
Comments
Post a Comment