mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-04-12 03:57:32 +00:00
fix: Logic for determining if there is a console output window, don't try to write to console if no console window
This commit is contained in:
parent
b3c1daaab5
commit
7784b99f75
1 changed files with 45 additions and 3 deletions
|
|
@ -25,9 +25,47 @@ namespace ICD.Common.Utils
|
|||
public static event EventHandler<StringEventArgs> OnConsolePrint;
|
||||
|
||||
private static readonly SafeCriticalSection s_Section;
|
||||
|
||||
private static readonly Regex s_NewLineRegex;
|
||||
|
||||
private static bool? s_IsConsoleApp;
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the application is being run from an interactive console,
|
||||
/// false if the application is being run as a headless service.
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public static bool IsConsoleApp
|
||||
{
|
||||
get
|
||||
{
|
||||
if (s_IsConsoleApp == null)
|
||||
{
|
||||
#if SIMPLSHARP
|
||||
s_IsConsoleApp = true;
|
||||
#else
|
||||
try
|
||||
{
|
||||
// Hack
|
||||
int unused = Console.WindowHeight;
|
||||
s_IsConsoleApp = true;
|
||||
|
||||
if (Console.Title.Length > 0)
|
||||
s_IsConsoleApp = true;
|
||||
|
||||
if (!Environment.UserInteractive)
|
||||
s_IsConsoleApp = false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
s_IsConsoleApp = false;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return s_IsConsoleApp.Value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Static constructor.
|
||||
/// </summary>
|
||||
|
|
@ -92,6 +130,8 @@ namespace ICD.Common.Utils
|
|||
CrestronConsole.PrintLine(fixedMessage);
|
||||
#else
|
||||
Trace.WriteLine(AnsiUtils.StripAnsi(fixedMessage));
|
||||
|
||||
if (IsConsoleApp)
|
||||
Console.WriteLine(fixedMessage);
|
||||
#endif
|
||||
}
|
||||
|
|
@ -134,6 +174,8 @@ namespace ICD.Common.Utils
|
|||
CrestronConsole.Print(fixedMessage);
|
||||
#else
|
||||
Trace.Write(AnsiUtils.StripAnsi(fixedMessage));
|
||||
|
||||
if (IsConsoleApp)
|
||||
Console.Write(fixedMessage);
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue