- Home /
await creates error with StreamReader
Using the code from this page: http://msdn.microsoft.com/en-us/library/system.io.streamreader.readasync.aspx
// filename is already setup with directory
char[] result;
StringBuilder builder = new StringBuilder();
using (StreamReader reader = File.OpenText(filename))
{
int readerLength = (int) (int)reader.BaseStream.Length;
result = new char[readerLength];
await reader.ReadAsync(result, 0, readerLength);
}
Unity gives me this error in the middle of the variable name 'readerLength' :
error CS1525: Unexpected Symbol '.', expecting ')',',',';','[', or '='
Any ideas why this doesn't work?
Answer by Abomb · Jul 15, 2013 at 10:11 AM
Unity only works with .NET framework Version 3.5 (it uses Mono 2.6), so language features from 4.0 and upward (like 'await') are not supported.
Thanks. Can you recommend any way to async read/write to text files or is using PlayerPrefs the only real option?
You can actually use Threads in Unity, you just have to make sure that you don't call any Unity methods from another thread (Unity will give error messages and not crash luckily). But it should absolutely be possible to read/write to a file on a separate Thread.
Of course, you don't have to use a separate thread to read or write a text file. e.g.
Read:
string myText= File.ReadAllText(myPath);
Write:
File.WriteAllText(myPath, myText);
Usually you'll want to catch exceptions etc as well.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Asyncchronous image loading 2 Answers
Unity Asynchronous Socket Client - Threading Problem 2 Answers
Async/Await function hangs when caller has infinite loop 1 Answer