Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
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 answers
Answer question

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 Report

0

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

over 4 years ago · Santiago Trujillo Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!