From b3c2fb82c0d247b21ccde4ee7574f4f3b54f3892 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 24 Apr 2024 13:15:57 +0200 Subject: [PATCH] Funcionando con Cronometro en milisegundos --- App.xaml.cs | 84 +++++++++++++++++++++++++++++++++++++++++++---------- gtpask.cs | 4 +-- 2 files changed, 70 insertions(+), 18 deletions(-) diff --git a/App.xaml.cs b/App.xaml.cs index b5edf53..4c0915a 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -5,6 +5,8 @@ using System; using System.Windows; using System.Windows.Forms; // Necesitas agregar una referencia a System.Windows.Forms using Application = System.Windows.Application; +using System.Diagnostics; // Asegúrate de incluir esta directiva para usar Stopwatch +using System.Threading.Tasks; namespace GTPCorrgir { @@ -14,46 +16,96 @@ namespace GTPCorrgir public partial class App : Application { gtpask GTP = new gtpask(); + NotifyIcon notificacion; + System.Windows.Forms.Timer timer; + int segundos = 0; + bool CorreccionFinalizada = false; + Stopwatch stopwatch = new Stopwatch(); + protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); + stopwatch.Start(); + if (System.Windows.Clipboard.ContainsText()) { GTP.TextoACorregir = System.Windows.Clipboard.GetText(); - - } - // Inicia y espera la tarea asíncrona + // Muestra notificación inicial y comienza el cronómetro en el hilo de la UI + MostrarNotificacion("Espera", "Corrigiendo texto..."); + IniciarCronometro(); + + // Ejecuta la tarea de corrección en un hilo secundario Task.Run(async () => { - await GTP.CorregirTexto(); - - // Asegúrate de cerrar la aplicación en el hilo de la UI - Dispatcher.Invoke(() => + try { - System.Windows.Clipboard.SetText(GTP.TextoCorregido); - MostrarNotificacion("Correccion Lista", GTP.TextoCorregido); - Application.Current.Shutdown(); - }); + await GTP.CorregirTexto(); + } + catch (Exception ex) + { + // Manejo de errores (puedes mostrar un mensaje de error si es necesario) + Console.WriteLine("Error durante la corrección de texto: " + ex.Message); + } + finally + { + // Asegura que el timer se detenga y la notificación se actualice en el hilo de la UI + Dispatcher.Invoke(() => + { + CorreccionFinalizada = true; + DetenerCronometro(); + if (GTP.TextoCorregido != null) + { + System.Windows.Clipboard.SetText(GTP.TextoCorregido); + MostrarNotificacion("Corrección Lista", GTP.TextoCorregido); + } + else + { + MostrarNotificacion("Error", "No se pudo corregir el texto."); + } + Application.Current.Shutdown(); + }); + } }); - - } - private void MostrarNotificacion(string titulo, string mensaje) { - NotifyIcon notificacion = new NotifyIcon + notificacion = new NotifyIcon { Icon = SystemIcons.Information, BalloonTipTitle = titulo, BalloonTipText = mensaje, Visible = true }; - notificacion.ShowBalloonTip(1000); // Muestra la notificación por 3000 milisegundos + notificacion.ShowBalloonTip(1000); + } + + private void IniciarCronometro() + { + timer = new System.Windows.Forms.Timer(); + timer.Interval = 1000; // 1000 milisegundos (1 segundo) + timer.Tick += ActualizarCronometro; + timer.Start(); + } + + private void ActualizarCronometro(object sender, EventArgs e) + { + if (!CorreccionFinalizada) { + segundos++; + notificacion.BalloonTipText = $"Texto en {GTP.IdiomaDetectado} pasados: {Math.Round(stopwatch.ElapsedMilliseconds/1000.0,1)} s"; + notificacion.ShowBalloonTip(1000); + } + } + + private void DetenerCronometro() + { + timer.Stop(); + timer.Dispose(); } } + } diff --git a/gtpask.cs b/gtpask.cs index 198da23..0075853 100644 --- a/gtpask.cs +++ b/gtpask.cs @@ -64,8 +64,8 @@ namespace GTPCorrgir TextoACorregir = "El resultado debe estar en " + IdiomaDetectado + ". " + Instrucciones + "\"" + TextoACorregir + "\""; - // TextoCorregido = await CallOpenAiApi(TextoACorregir); - TextoCorregido = await CallOllamaApi(TextoACorregir); + TextoCorregido = await CallOpenAiApi(TextoACorregir); + //TextoCorregido = await CallOllamaApi(TextoACorregir); // Elimina comillas al principio y al final si existen TextoCorregido = TextoCorregido.Trim('\"');