当前位置: 首页>>代码示例>>C#>>正文


C# BufferedStream.EnsureAsyncActiveSemaphoreInitialized方法代码示例

本文整理汇总了C#中System.IO.BufferedStream.EnsureAsyncActiveSemaphoreInitialized方法的典型用法代码示例。如果您正苦于以下问题:C# BufferedStream.EnsureAsyncActiveSemaphoreInitialized方法的具体用法?C# BufferedStream.EnsureAsyncActiveSemaphoreInitialized怎么用?C# BufferedStream.EnsureAsyncActiveSemaphoreInitialized使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.BufferedStream的用法示例。


在下文中一共展示了BufferedStream.EnsureAsyncActiveSemaphoreInitialized方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FlushAsyncInternal

    private static async Task FlushAsyncInternal(CancellationToken cancellationToken,
                                                 BufferedStream _this, Stream stream, Int32 writePos, Int32 readPos, Int32 readLen) {
        
        // We bring instance fields down as local parameters to this async method becasue BufferedStream is derived from MarshalByRefObject.
        // Field access would be from the async state machine i.e., not via the this pointer and would require runtime checking to see
        // if we are talking to a remote object, which is currently very slow 

        Contract.Assert(stream != null);        
        
        SemaphoreSlim sem = _this.EnsureAsyncActiveSemaphoreInitialized();
        await sem.WaitAsync().ConfigureAwait(false);
        try {

            if (writePos > 0) {

                await _this.FlushWriteAsync(cancellationToken).ConfigureAwait(false);
                Contract.Assert(_this._writePos == 0 && _this._readPos == 0 && _this._readLen == 0);
                return;
            }        

            if (readPos < readLen) {

                // If the underlying stream is not seekable AND we have something in the read buffer, then FlushRead would throw.
                // We can either throw away the buffer resulting in date loss (!) or ignore the Flush. (We cannot throw becasue it
                // would be a breaking change.) We opt into ignoring the Flush in that situation.
                if (!stream.CanSeek)
                    return;                                   

                _this.FlushRead();  // not async; it uses Seek, but there's no SeekAsync

                // User streams may have opted to throw from Flush if CanWrite is false (although the abstract Stream does not do so).
                // However, if we do not forward the Flush to the underlying stream, we may have problems when chaining several streams.
                // Let us make a best effort attempt:
                if (stream.CanRead || stream is BufferedStream)
                    await stream.FlushAsync(cancellationToken).ConfigureAwait(false);

                Contract.Assert(_this._writePos == 0 && _this._readPos == 0 && _this._readLen == 0);
                return;
            }            

            // We had no data in the buffer, but we still need to tell the underlying stream to flush.
            if (stream.CanWrite || stream is BufferedStream)
                await stream.FlushAsync(cancellationToken).ConfigureAwait(false);

            // There was nothing in the buffer:
            Contract.Assert(_this._writePos == 0 && _this._readPos == _this._readLen);

        } finally {
            sem.Release();
        }
    }
开发者ID:l1183479157,项目名称:coreclr,代码行数:51,代码来源:BufferedStream.cs


注:本文中的System.IO.BufferedStream.EnsureAsyncActiveSemaphoreInitialized方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。