我已经制作了一个“可以运行”的Windows服务,但是在启动时遇到了问题

问题描述

所以不久前,我做了一个Windows服务,该服务读出一个串行端口,根据返回的值,它将改变系统容量。问题是,当我运行该服务时,它停留在启动模式下,但仍按其应有的方式运行。但是如果您将其设置为“启动时自动启动”,它将无法正常工作。这是我第一次进行Windows服务,因此可能会有很多问题。

这是正在运行的代码

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