我做了一个 Windows 服务 "works" 但在启动时卡住了

I have made a Windows service that "works" but is stuck in at starting up

所以不久前我做了一个 windows 服务,它读取串行端口并根据返回的值改变系统音量。问题是当我 运行 服务时它卡在启动模式但仍然执行它应该执行的操作。但如果您将其设置为“启动时自动启动”,它将不起作用。这是我第一次做 windows 服务所以它可能有很多问题。

这是 运行ning 的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using AudioSwitcher.AudioApi.CoreAudio;
using System.IO.Ports;
using System.Threading;

namespace audio_controller_WS
{
    public partial class AudioContorllerWS : ServiceBase
    {
        static bool _continue;
        static SerialPort _serialPort;
        public AudioContorllerWS()
        {
            InitializeComponent();

           


        }
        public static void Read()
        {
            CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice;
            while (_continue)
            {
                try
                {
                    String message = _serialPort.ReadLine();
                    int convert = int.Parse(message);
                    defaultPlaybackDevice.Volume = convert;
                }
                catch (Exception)
                { }

            }
        }
        protected override void OnStart(string[] args)
        {
                      // string message;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            Thread readThread = new Thread(Read);

            // Create a new SerialPort object with default settings.
            _serialPort = new SerialPort();

            // Allow the user to set the appropriate properties.
            _serialPort.PortName = "COM10";
            _serialPort.BaudRate = 9600;
            _serialPort.DataBits = 8;
            // Set the read/write timeouts
            _serialPort.ReadTimeout = 500;
            _serialPort.WriteTimeout = 500;

            _serialPort.Open();
            _continue = true;
            readThread.Start();
            readThread.Join();
            _serialPort.Close();

        }
        protected override  void OnContinue()
        {

        }


        protected override void OnStop()
        {
        }
    }
}


我知道我的代码不应该 运行在启动时启动,但我无法正常工作。

您需要让 OnStart 方法退出。试试这个:

public partial class AudioContorllerWS : ServiceBase
{
    static bool _continue;
    static SerialPort _serialPort;
    static Thread readThread;
    
    public AudioContorllerWS()
    {
        InitializeComponent();         
    }
    
    public static void Read()
    {
        CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice;
        while (_continue)
        {
            try
            {
                String message = _serialPort.ReadLine();
                int convert = int.Parse(message);
                defaultPlaybackDevice.Volume = convert;
            }
            catch (Exception)
            { }
        }
    }
    
    protected override void OnStart(string[] args)
    {
        // string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        readThread = new Thread(Read);

        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort.PortName = "COM10";
        _serialPort.BaudRate = 9600;
        _serialPort.DataBits = 8;
        // Set the read/write timeouts
        _serialPort.ReadTimeout = 500;
        _serialPort.WriteTimeout = 500;

        _serialPort.Open();
        _continue = true;
        readThread.Start();
        // don't join
    }

    protected override  void OnContinue()
    {
    }


    protected override void OnStop()
    {
        _continue = false;
        readThread.Join(timeout);
        _serialPort.Close();
        // abort it if it hasn't exited
    }
}