Tino
Tino

Reputation: 229

Thread safe async file writе with FileIO.ReadTextAsync

How I can use FileIO.ReadTextAsync in windows universal apps so that it will be thread safe? Because trying something similar like

lock(lockObject)
{
    var data = await ReadTextAsync(fileName);
}

but using await in lock is not supported =(.

Upvotes: 1

Views: 140

Answers (2)

thoean
thoean

Reputation: 1116

It's not supported because there are no (managed) threads during an I/O call, and the current method will be continued later, potentially on another thread.

Lock is a synchronous concept, and since you're serializing access already, can you just call ReadText instead of ReadTextAsync? Do you really benefit from async in your case?

Upvotes: 1

usr
usr

Reputation: 171216

Use a SemaphoreSlim and the WaitAsync method.

Upvotes: 1

Related Questions