C # console program forever waiting for an event

I have a simple C # console application that attaches to an event. I need the program to continue to run continuously so that it can respond to the event. What is the correct way to work it?

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;
            }
        }
    }
}
+8
source share
3 answers

You can call Console.ReadLine()(if you want to complete the keystroke) or simply Thread.Sleep(Timeout.Infinite).

+15
source

In the case of the main method, asyncyou can also useawait Task.Delay(-1);

0
source

while :

while(!eventFired) {}

:

private bool eventFired = false;

, , make:

eventFired = true;

( , , , , , . readkey)

: , .

-2
source

All Articles