Does visual profiler not work with console clearing code?

I have a C# .NET program P/Invoking into a CUDA DLL. When I use the Visual Profiler to profile the program, an exception is thrown right after the first Console.Clear() line in the C# file and the program is terminated:

Unhandled Exception: System.IO.IOException: The handle is invalid.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.Console.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)
   at System.Console.Clear()

This only happens when the program is profiled. Removing the Console.Clear() or adding a try catch block around it no longer causes the program to terminate, but it doesn’t clear the console either.

If I move the console clearing to the CUDA host code using system(“cls”), it still doesn’t clear the console and just outputs a single form feed character (12) when profiled.