Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

90
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

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

over 4 years ago · Santiago Trujillo Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda