未验证 提交 d99ddd06 编写于 作者: R Roman Marusyk 提交者: GitHub

Throw an ObjectDisposedException instead of InvalidOperationException after...

Throw an ObjectDisposedException instead of InvalidOperationException after Process object has been disposed (#46593)

fixes #45149
上级 9a3e8667
......@@ -332,4 +332,4 @@
<data name="InvalidSysctl" xml:space="preserve">
<value>sysctl {0} failed with {1} error.</value>
</data>
</root>
</root>
\ No newline at end of file
......@@ -683,6 +683,7 @@ public StreamWriter StandardInput
{
get
{
CheckDisposed();
if (_standardInput == null)
{
throw new InvalidOperationException(SR.CantGetStandardIn);
......@@ -700,6 +701,7 @@ public StreamReader StandardOutput
{
get
{
CheckDisposed();
if (_standardOutput == null)
{
throw new InvalidOperationException(SR.CantGetStandardOut);
......@@ -725,6 +727,7 @@ public StreamReader StandardError
{
get
{
CheckDisposed();
if (_standardError == null)
{
throw new InvalidOperationException(SR.CantGetStandardError);
......@@ -1150,10 +1153,7 @@ private SafeProcessHandle GetOrOpenProcessHandle()
if (!_haveProcessHandle)
{
//Cannot open a new process handle if the object has been disposed, since finalization has been suppressed.
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
CheckDisposed();
SetProcessHandle(GetProcessHandle());
}
......@@ -1224,10 +1224,7 @@ public bool Start()
}
//Cannot start a new process and store its handle if the object has been disposed, since finalization has been suppressed.
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
CheckDisposed();
SerializationGuard.ThrowIfDeserializationInProgress("AllowProcessCreation", ref s_cachedSerializationSwitch);
......@@ -1586,6 +1583,7 @@ public void BeginErrorReadLine()
/// </devdoc>
public void CancelOutputRead()
{
CheckDisposed();
if (_output != null)
{
_output.CancelOperation();
......@@ -1606,6 +1604,7 @@ public void CancelOutputRead()
/// </devdoc>
public void CancelErrorRead()
{
CheckDisposed();
if (_error != null)
{
_error.CancelOperation();
......@@ -1656,6 +1655,16 @@ internal void ErrorReadNotifyUser(string? data)
}
}
/// <summary>Throws a System.ObjectDisposedException if the Proces was disposed</summary>
/// <exception cref="System.ObjectDisposedException">If the Proces has been disposed.</exception>
private void CheckDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
}
/// <summary>
/// This enum defines the operation mode for redirected process stream.
/// We don't support switching between synchronous mode and asynchronous mode.
......
......@@ -1467,6 +1467,56 @@ public void Start_Disposed_ThrowsObjectDisposedException()
Assert.Throws<ObjectDisposedException>(() => process.Start());
}
[Fact]
public void StandardInput_Disposed_ThrowsObjectDisposedException()
{
var process = new Process();
process.StartInfo.FileName = "Nothing";
process.Dispose();
Assert.Throws<ObjectDisposedException>(() => process.StandardInput);
}
[Fact]
public void StandardError_Disposed_ThrowsObjectDisposedException()
{
var process = new Process();
process.StartInfo.FileName = "Nothing";
process.Dispose();
Assert.Throws<ObjectDisposedException>(() => process.StandardError);
}
[Fact]
public void StandardOutput_Disposed_ThrowsObjectDisposedException()
{
var process = new Process();
process.StartInfo.FileName = "Nothing";
process.Dispose();
Assert.Throws<ObjectDisposedException>(() => process.StandardOutput);
}
[Fact]
public void CancelOutputRead_Disposed_ThrowsObjectDisposedException()
{
var process = new Process();
process.StartInfo.FileName = "Nothing";
process.Dispose();
Assert.Throws<ObjectDisposedException>(() => process.CancelOutputRead());
}
[Fact]
public void CancelErrorRead_Disposed_ThrowsObjectDisposedException()
{
var process = new Process();
process.StartInfo.FileName = "Nothing";
process.Dispose();
Assert.Throws<ObjectDisposedException>(() => process.CancelErrorRead());
}
[Fact]
[PlatformSpecific(TestPlatforms.Linux | TestPlatforms.Windows)] // Expected process HandleCounts differs on OSX
public void TestHandleCount()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册