加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#控制台程序永远等待事件

发布时间:2020-12-15 07:59:36 所属栏目:百科 来源:网络整理
导读:我有一个简单的C#控制台应用程序附加到事件.我需要程序继续运行,以便它可以响应事件.什么是保持运行的正确方法? 这是我的申请: using System;using NAudio.CoreAudioApi;namespace MaxVolume{ class Program { private const float DesiredLevel = -15; pr
我有一个简单的C#控制台应用程序附加到事件.我需要程序继续运行,以便它可以响应事件.什么是保持运行的正确方法?

这是我的申请:

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;
            }
        }
    }
}

解决方法

您可以调用Console.ReadLine()(如果要终止击键),或者只调用Thread.Sleep(Timeout.Infinite).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读