Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public IntPtr MainWindowHandle
EnsureState(State.IsLocal | State.HaveId);
_mainWindowHandle = ProcessManager.GetMainWindowHandle(_processId);

_haveMainWindow = true;
_haveMainWindow = _mainWindowHandle != IntPtr.Zero;
}
return _mainWindowHandle;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public void Kill()
private void RefreshCore()
{
_signaled = false;
_haveMainWindow = false;
}

/// <summary>Additional logic invoked when the Process is closed.</summary>
Expand Down
35 changes: 35 additions & 0 deletions src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.DotNet.XUnitExtensions;
using Microsoft.Win32.SafeHandles;
using Xunit;
using Xunit.Sdk;
Expand Down Expand Up @@ -1509,6 +1510,40 @@ public void MainWindowHandle_GetNotStarted_ThrowsInvalidOperationException()
var process = new Process();
Assert.Throws<InvalidOperationException>(() => process.MainWindowHandle);
}

[Fact]
[OuterLoop]
[Trait(XunitConstants.Category, XunitConstants.IgnoreForCI)] // Pops UI
[PlatformSpecific(TestPlatforms.Windows)]
public void MainWindowHandle_GetWithGui_ShouldRefresh_Windows()
{
const string ExePath = "notepad.exe";
Assert.True(IsProgramInstalled(ExePath));

using (Process process = Process.Start(ExePath))
{
try
{
for (int attempt = 0; attempt < 50; ++attempt)
{
process.Refresh();
if (process.MainWindowHandle != IntPtr.Zero)
{
break;
}

Thread.Sleep(100);
}

Assert.NotEqual(IntPtr.Zero, process.MainWindowHandle);
}
finally
{
process.Kill();
Assert.True(process.WaitForExit(WaitInMS));
}
}
}

[Fact]
public void MainWindowTitle_NoWindow_ReturnsEmpty()
Expand Down