Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

96
Visualizações
C# console program wait forever for event

I have a simple C# console application that attaches to an event. I need the program to keep running continuously so it can respond to the event. What is the right way to keep it running?

Here is my application:

using System;
using NAudio.CoreAudioApi;

namespace MaxVolume
{
    class Program
    {
        private const float DesiredLevel = -15;
        private static MMDevice _device;

        static void Main(string[] args)
        {
            MMDeviceEnumerator mmde = new MMDeviceEnumerator();
            _device = mmde.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);

            _device.AudioEndpointVolume.MasterVolumeLevel = DesiredLevel;

            _device.AudioEndpointVolume.OnVolumeNotification += SetVolume;
        }

        static void SetVolume(AudioVolumeNotificationData data)
        {
            if (Math.Abs(data.MasterVolume - DesiredLevel) > 0.1)
            {
                _device.AudioEndpointVolume.MasterVolumeLevel = DesiredLevel;
            }
        }
    }
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can call Console.ReadLine() (if you want to terminate on keystroke), or simply Thread.Sleep(Timeout.Infinite).

over 4 years ago · Santiago Trujillo Relatório

0

In case of async main method, one could also use await Task.Delay(-1);

over 4 years ago · Santiago Trujillo Relatório

0

You can just create a while-loop like so:

while(!eventFired) {}

and then have a field:

private bool eventFired = false;

and then finally when the event is fired make:

eventFired = true;

(if you want it to exit after the event was fired once, that is. if it should run forever see the other answers with sleep and readkey)

NOTE: this is using up a CPU and shouldn't be used in any production, but in a quick-and-dirty test setting it might be worth it.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda