public static void TestWait(bool approach, int timeout)
{
GCNotificationStatus result = GCNotificationStatus.Failed;
Thread cancelProc = null;
// Since we need to test an infinite (or very large) wait but the API won't return, spawn off a thread which
// will cancel the wait after a few seconds
//
bool cancelTimeout = (timeout == -1) || (timeout > 10000);
GC.RegisterForFullGCNotification(20, 20);
try
{
if (cancelTimeout)
{
cancelProc = new Thread(new ThreadStart(CancelProc));
cancelProc.Start();
}
if (approach)
result = GC.WaitForFullGCApproach(timeout);
else
result = GC.WaitForFullGCComplete(timeout);
}
catch (Exception e)
{
Assert.True(false, $"({approach}, {timeout}) Error - Unexpected exception received: {e.ToString()}");
}
finally
{
if (cancelProc != null)
cancelProc.Join();
}
if (cancelTimeout)
{
Assert.True(result == GCNotificationStatus.Canceled, $"({approach}, {timeout}) Error - WaitForFullGCApproach result not Cancelled");
}
else
{
Assert.True(result == GCNotificationStatus.Timeout, $"({approach}, {timeout}) Error - WaitForFullGCApproach result not Timeout");
}
}