微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决我已经制作了一个“可以运行”的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
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。