using EFCDesk.Classes; using EFCDesk.Forms; using System.Threading; using System.Diagnostics; using System.Reflection; namespace EFCDesk { public static class Globales { public static MonitorCarpetas gMonitor; public static ProcessLogger logger = new ProcessLogger(); public static ConfiguracionJSON configJson = ConfiguracionJSON.LoadFromJson(); public static string accesToken = ""; public static string refresToken = ""; public static string VersionApp = "1.0.0"; public static ApiClient ApiClientLarge { get; set; } = null!; public static ApiClient ApiClient { get; set; } = null!; } internal static class Program { /// /// The main entry point for the application. /// [STAThread] static async Task Main() { /**/ Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var fileVersionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location); Globales.VersionApp = fileVersionInfo.FileVersion ?? "1.0.0"; string carpetaBase = @"C:\EFC"; if (!Directory.Exists(Globales.configJson.FolderExpediente)) { string folder = "C:" + Path.DirectorySeparatorChar + "EFC"; // Asigna el valor al objeto de configuraci�n Globales.configJson.FolderExpediente = folder; // Guarda los cambios en el archivo JSON Globales.configJson.SaveToJson(); Directory.CreateDirectory(folder); carpetaBase = folder; } else { carpetaBase = Globales.configJson.FolderExpediente; } Properties.Settings.Default.urlEFC = Globales.configJson.DominioExp; Properties.Settings.Default.Save(); Globales.ApiClientLarge = new ApiClient( timeout: TimeSpan.FromSeconds(600), maxRetries: 3, retryDelay: TimeSpan.FromSeconds(60)); Globales.ApiClient = new ApiClient( timeout: TimeSpan.FromSeconds(10), maxRetries: 3, retryDelay: TimeSpan.FromSeconds(2)); Globales.gMonitor = new MonitorCarpetas( carpetaBase: carpetaBase, maxConcurrent: 1, perFileTimeoutSeconds: 30, maxReintentos: 10 ); bool Esvalido = Utils.Util.StringsValidos(Globales.configJson.UsuarioExp, Globales.configJson.PasswordExp); if (!Esvalido) { MessageBox.Show("Debe ingresar Usuario y contraseña.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Run(new Login()); } else { // Intentar login automático para obtener tokens bool loginExitoso = await Utils.Util.Login(Globales.configJson.UsuarioExp ?? "", Globales.configJson.PasswordExp ?? ""); if (!loginExitoso) { MessageBox.Show("Error en el inicio de sesión automático. Por favor, verifique sus credenciales.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Run(new Login()); return; } // Antes de llamar a cualquier m�todo sobre Globales.gMonitor, verifica que no sea null if (Globales.gMonitor != null && !Globales.gMonitor.ExisteConfiguracionExpediente()) { Application.Run(new Login()); } else { if (Globales.gMonitor != null) { Application.Run(new FormMain(Globales.gMonitor)); } else { MessageBox.Show("Error interno: el monitor de carpetas no está inicializado.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } } // Al terminar la aplicaci�n, liberar el recurso manualmente Globales.gMonitor?.Dispose(); Globales.ApiClientLarge?.Dispose(); Globales.ApiClient?.Dispose(); } } } }