c# - Trying to use async method TextReader.ReadBlockAsync, but getting "the 'await' operator can only be used within an async method" -


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

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -