首页 > 解决方案 > 可以调用无限运行的 Python 脚本的 Windows 服务(在 C# 中)?

问题描述

据我了解,当您运行 Windows 服务时,它会调用“onStart()”,并会说该服务正在任务管理器中“启动”,直到“onStart()”方法完全运行,然后它会说“跑步”。我想创建一个线程来启动蓝牙广告脚本(用 Python3 编写),只要它没有连接到设备,它就会不断地做广告。当我尝试创建一个线程并运行脚本时,该服务通常只在任务管理器中显示“正在启动”,而实际上从未显示“正在运行”,因为我在“onStart()”中调用以启动脚本的线程永远不会完成我猜有没有办法让服务启动,只有在它完成执行'onStart()'之后才启动线程?

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 System.IO;
using System.Timers;
using System.Threading;
using System.Security.Permissions;

namespace WindowsService1
{
//--------------------------------------------------------------------------------------------------------------------------------------------------
public partial class Service1 : ServiceBase
{
    //-------------------------------------------------------------------------
    public Service1() { InitializeComponent(); }

    //-------------------------------------------------------------------------
    private Boolean _started;
    public event System.EventHandler serviceChanged;
    Service1 serv = new Service1();
    static ThreadStart start = new ThreadStart(Adv_Py_Script);
    Thread Adv_Py_ScriptThread = new Thread(start);
    //Start_Adv s = new Start_Adv();
    //Thread thread1 = new Thread(new ThreadStart(s.Adv_Py_Script));



    //-------------------------------------------------------------------------
    protected override void OnStart(string[] args)
    {
        isStarted = true;



        Thread.Sleep(5000);

    }
    //-------------------------------------------------------------------------
    protected virtual void onServiceChanged() { serviceChanged?.Invoke(this, EventArgs.Empty); }

    //-------------------------------------------------------------------------
    public Boolean isStarted
    {
        get { return _started; }
        set
            {
            _started = value;
            onServiceChanged();
            }
    }

    //-------------------------------------------------------------------------
    protected override void OnStop()
    {
        isStarted = false;

        serv.killPyThread(Adv_Py_ScriptThread);
        base.OnStop();
        // wait for threads to stop
        Adv_Py_ScriptThread.Join(60);

        try
        {
            string error = "";
           // Messaging.SMS.SendSMSTextAsync("5555555555", "Messaging Service stopped on " + System.Net.Dns.GetHostName(), ref error);
        }
        catch
        {
            // yes eat exception if text failed
        }

}

    //-------------------------------------------------------------------------
    [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
    public void killPyThread(Thread thread)
    {
        thread.Interrupt();
        thread.Abort();
    }

    //-------------------------------------------------------------------------

    private static void Adv_Py_Script()
    {
        string fileName = @"C:\Users\bakere1\A19149\Projects\BLE_Advertiser.py";
        Process p = new Process();
        p.StartInfo = new ProcessStartInfo(@"C:\Python36_64\python.exe", fileName)
        {
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };
        p.Start();
    }
}

标签: c#pythonwindowsmultithreadingwindows-services

解决方案


虽然可能存在其他问题,但第一个问题是您的服务(可能)运行的本地服务帐户没有对该位置的权限C:\Users\bakere1\A19149\Projects\

如果您将“运行方式”帐户更改为具有权限的帐户,您会走得更远。我不是一个大蟒蛇极客,但如果你的 py 应用程序需要任何类型的用户 I/O,它将失败,因为没有分配给它的控制台会话。

此外,如果您有漫游配置文件,该服务会在用户文件夹可用之前启动,因此它会挂起或失败。


推荐阅读