System.IO.StreamReader.ReadLineAsync C# (CSharp) Method

ReadLineAsync() public method

public ReadLineAsync ( ) : Task
return Task
        public override Task<string> ReadLineAsync()
        {
            // If we have been inherited into a subclass, the following implementation could be incorrect
            // since it does not call through to Read() which a subclass might have overridden.  
            // To be safe we will only use this implementation in cases where we know it is safe to do so,
            // and delegate to our base class (which will call into Read) when we are not sure.
            if (GetType() != typeof(StreamReader))
            {
                return base.ReadLineAsync();
            }

            if (_stream == null)
            {
                throw new ObjectDisposedException(null, SR.ObjectDisposed_ReaderClosed);
            }

            CheckAsyncTaskInProgress();

            Task<string> task = ReadLineAsyncInternal();
            _asyncReadTask = task;

            return task;
        }

Usage Example

Example #1
0
        private async void ClientWork(object state)
        {
            int clientId = (int)state;
            using (var channel = this.connection.ConnectChannel())
            {
                using (StreamWriter writer = new StreamWriter(channel))
                {
                    using (StreamReader reader = new StreamReader(channel))
                    {
                        string request = string.Format("Hello from {0}", clientId);
                        string response;
                        await writer.WriteLineAsync(request);
                        await writer.FlushAsync();
                        response = await reader.ReadLineAsync();
                        await writer.WriteLineAsync(request);
                        await writer.FlushAsync();
                        response = await reader.ReadLineAsync();
                        await channel.StopSendingAsync();
                        await channel.FlushAsync();
                        if (reader.EndOfStream)
                        {
                            Console.WriteLine("Client feel right!");
                        }
                    }
                }
            }

            if (Interlocked.Decrement(ref pendingCount) == 0)
            {
                lock (termLock)
                {
                    Monitor.Pulse(termLock);
                }
            }
        }
All Usage Examples Of System.IO.StreamReader::ReadLineAsync