public Task Send(Func<object, Task> send, object state)
{
lock (_lockObj)
{
if (_error != null)
{
throw _error;
}
// If the queue is closed then stop sending
if (_state == StreamState.Closed)
{
throw new InvalidOperationException(Resources.Error_StreamClosed);
}
var context = new SendContext(this, send, state);
if (_initializeDrainTask != null && !_initializeDrainTask.IsCompleted)
{
// Wait on the draining of the queue before proceeding with the send
// NOTE: Calling .Wait() here is safe because the task wasn't created on an ASP.NET request thread
// and thus has no captured sync context
_initializeDrainTask.Wait();
}
if (UsingTaskQueue)
{
Task task = _queue.Enqueue(Send, context);
if (task == null)
{
// The task is null if the queue is full
throw new InvalidOperationException(Resources.Error_TaskQueueFull);
}
// Always observe the task in case the user doesn't handle it
return task.Catch(_logger);
}
return Send(context);
}
}