-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
87 lines (81 loc) · 3.37 KB
/
Copy pathApp.xaml.cs
File metadata and controls
87 lines (81 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Windows;
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;
namespace YAMAGoya
{
/// <summary>
/// Interaction logic for App.xaml.
/// This class serves as the entry point of the application.
/// It determines whether to run in GUI mode or command-line mode based on startup arguments.
/// </summary>
[SuppressMessage("Design", "CA1515:Type can be made internal", Justification = "App must be public for XAML binding to work properly.")]
public partial class App : Application
{
/// <summary>
/// Gets or sets a value indicating whether the application is running in GUI mode.
/// When command-line arguments are provided, this property remains false.
/// </summary>
public static bool IsGuiMode { get; set; }
/// <summary>
/// Attaches the calling process to the console of an existing process.
/// Pass -1 to attach to the parent process's console.
/// </summary>
/// <param name="processId">The process ID to attach to.</param>
/// <returns>True if successful; otherwise, false.</returns>
[DllImport("Kernel32.dll")]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
private static extern bool AttachConsole(int processId);
/// <summary>
/// Static constructor. Attempts to attach the process to the parent's console.
/// </summary>
static App()
{
AttachConsole(-1);
}
/// <summary>
/// Called when the application starts.
/// If command-line arguments are provided, the application runs in command-line mode.
/// Otherwise, it launches the GUI.
/// </summary>
/// <param name="e">Startup event arguments.</param>
protected override void OnStartup(StartupEventArgs e)
{
// Ensure that the StartupEventArgs is not null.
ArgumentNullException.ThrowIfNull(e);
base.OnStartup(e);
// If command-line arguments are provided, run in command-line mode.
if (e.Args != null && e.Args.Length > 0)
{
Console.WriteLine("Running in command-line mode...");
try
{
CommandLineProcessor.Process(e.Args);
}
catch (ArgumentException ex)
{
MessageBox.Show($"Argument error: {ex.Message}", "YAMAGoya Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
catch (InvalidOperationException ex)
{
MessageBox.Show($"Operation error: {ex.Message}", "YAMAGoya Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
// Rethrow any other exceptions.
catch
{
MessageBox.Show("An unexpected error occurred. Please check the logs for more details.", "YAMAGoya Error", MessageBoxButton.OK, MessageBoxImage.Error);
throw;
}
Shutdown();
}
else
{
IsGuiMode = true;
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
}
}
}
}