private async Task WriteAsyncInternal(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
// To avoid a race with a stream's position pointer & generating race
// conditions with internal buffer indexes in our own streams that
// don't natively support async IO operations when there are multiple
// async requests outstanding, we will block the application's main
// thread if it does a second IO request until the first one completes.
SemaphoreSlim semaphore = AsyncActiveSemaphore;
await semaphore.WaitAsync().ForceAsync();
try
{
await WriteAsyncCore(buffer, offset, count, cancellationToken, useAsync: true);
}
finally
{
semaphore.Release();
}
}