System.IO.StreamWriter.WriteAsync C# (CSharp) Method

WriteAsync() public method

public WriteAsync ( char value ) : System.Threading.Tasks.Task
value char
return System.Threading.Tasks.Task
        public override Task WriteAsync(char value)
        {
            // If we have been inherited into a subclass, the following implementation could be incorrect
            // since it does not call through to Write() 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 Write) when we are not sure.
            if (GetType() != typeof(StreamWriter))
            {
                return base.WriteAsync(value);
            }

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

            CheckAsyncTaskInProgress();

            Task task = WriteAsyncInternal(this, value, _charBuffer, _charPos, _charLen, CoreNewLine, _autoFlush, appendNewLine: false);
            _asyncWriteTask = task;

            return task;
        }

Same methods

StreamWriter::WriteAsync ( char buffer, int index, int count ) : System.Threading.Tasks.Task
StreamWriter::WriteAsync ( string value ) : System.Threading.Tasks.Task

Usage Example

Example #1
0
		protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
		{
			using (var writer = new StreamWriter(stream))
			{
                await writer.WriteAsync("data: { \"Type\": \"Heartbeat\" }\r\n\r\n");
				await writer.FlushAsync();
							
				while (Connected)
				{
					try
					{
						var result = await manualResetEvent.WaitAsync(5000);
						if (Connected == false)
							return;

						if (result == false)
						{
                            await writer.WriteAsync("data: { \"Type\": \"Heartbeat\" }\r\n\r\n");
							await writer.FlushAsync();

                            if (lastMessageEnqueuedAndNotSent != null)
                            {
								await SendMessage(lastMessageEnqueuedAndNotSent, writer);
                            }
							continue;
						}

						manualResetEvent.Reset();

						object message;
						while (msgs.TryDequeue(out message))
						{
							if (CoolDownWithDataLossInMiliseconds > 0 && Environment.TickCount - lastMessageSentTick < CoolDownWithDataLossInMiliseconds)
                            {
                                lastMessageEnqueuedAndNotSent = message;
                                continue;
                            }

							await SendMessage(message, writer);
						}
					}
					catch (Exception e)
					{
						Connected = false;
						log.DebugException("Error when using events transport", e);
						Disconnected();
						try
						{
							writer.WriteLine(e.ToString());
						}
						catch (Exception)
						{
							// try to send the information to the client, okay if they don't get it
							// because they might have already disconnected
						}

					}
				}
			}
		}
All Usage Examples Of System.IO.StreamWriter::WriteAsync