Hi,
Would it be possible to add the above method to the ShellStream class?
I have an application that does a "read and collect until .... found". During developement of the app I discovered that doing a continuous "if (shell.DataAvailable) shell.Read(...)" doesn't return anything because the correct threads a not getting time to do any work.
The waithandle that is used in the ShellStream class isn't publicly accessible (which is good) but this handle is required to complete my code.
Below a possible implementation for this method.
public override int Read(byte[] buffer, int offset, int count, TimeSpan timeout)
{
var i = 0;
bool wait = false;
lock (this._incoming)
{
wait = (this._incoming.Count < count);
}
if (wait)
{
if (timeout != null)
this._dataReceived.WaitOne(timeout);
else
this._dataReceived.WaitOne();
}
lock (this._incoming)
{
for (; i < count && this._incoming.Count > 0; i++)
{
buffer[offset + i] = this._incoming.Dequeue();
}
}
return i;
}
Alternatively; if an Expect() method could be written that Captures all from start of Expect until the expected is found would also be nice.
Regards,
Erik
Would it be possible to add the above method to the ShellStream class?
I have an application that does a "read and collect until .... found". During developement of the app I discovered that doing a continuous "if (shell.DataAvailable) shell.Read(...)" doesn't return anything because the correct threads a not getting time to do any work.
The waithandle that is used in the ShellStream class isn't publicly accessible (which is good) but this handle is required to complete my code.
Below a possible implementation for this method.
public override int Read(byte[] buffer, int offset, int count, TimeSpan timeout)
{
var i = 0;
bool wait = false;
lock (this._incoming)
{
wait = (this._incoming.Count < count);
}
if (wait)
{
if (timeout != null)
this._dataReceived.WaitOne(timeout);
else
this._dataReceived.WaitOne();
}
lock (this._incoming)
{
for (; i < count && this._incoming.Count > 0; i++)
{
buffer[offset + i] = this._incoming.Dequeue();
}
}
return i;
}
Alternatively; if an Expect() method could be written that Captures all from start of Expect until the expected is found would also be nice.
Regards,
Erik