Last active
October 2, 2023 17:47
-
-
Save alyleite/a02c3bca4ed81dbfdf6f03defef1ac98 to your computer and use it in GitHub Desktop.
isRunning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
class Program | |
{ | |
private static bool isRunning = false; | |
private static bool isMonitoring = false; | |
private static DateTime lastExecutionTime; | |
static void Main(string[] args) | |
{ | |
// Crie uma nova thread para executar o código de verificação da API | |
Thread apiThread = new Thread(ApiCheckingThread); | |
apiThread.Start(); | |
// Inicie a thread de monitoramento | |
Thread monitoringThread = new Thread(MonitoringThread); | |
monitoringThread.Start(); | |
// Aguarde o programa continuar em execução | |
Console.WriteLine("Pressione Enter para sair."); | |
Console.ReadLine(); | |
} | |
private static void ApiCheckingThread() | |
{ | |
do | |
{ | |
// Registre o tempo de início da execução | |
lastExecutionTime = DateTime.Now; | |
// Verifique se a thread já está em execução | |
if (isRunning) | |
{ | |
Console.WriteLine("Consulta anterior ainda em andamento. Ignorando esta verificação."); | |
} | |
else | |
{ | |
isRunning = true; | |
try | |
{ | |
// Aqui você pode fazer a chamada à API e verificar os pedidos não impressos | |
Console.WriteLine("Verificando pedidos não impressos na API..."); | |
// Simule uma consulta demorada | |
Thread.Sleep(TimeSpan.FromSeconds(10)); // Simulando uma consulta que leva menos de 30 segundos | |
Console.WriteLine("Verificação concluída."); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Ocorreu um erro ao verificar a API: {ex.Message}"); | |
} | |
finally | |
{ | |
isRunning = false; | |
} | |
} | |
// Aguarde 30 segundos antes de fazer a próxima verificação | |
Thread.Sleep(TimeSpan.FromSeconds(30)); | |
} while (true); // Este loop executará infinitamente | |
} | |
private static void MonitoringThread() | |
{ | |
do | |
{ | |
// Calcule o tempo desde a última execução | |
TimeSpan elapsedTime = DateTime.Now - lastExecutionTime; | |
if (elapsedTime.TotalSeconds < 30) | |
{ | |
Console.WriteLine($"A thread está em execução por {elapsedTime.TotalSeconds} segundos."); | |
} | |
else | |
{ | |
Console.WriteLine("A thread está executando dentro do intervalo esperado."); | |
} | |
// Aguarde 10 segundos antes de verificar novamente | |
Thread.Sleep(TimeSpan.FromSeconds(10)); | |
} while (true); // Este loop executará infinitamente | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment