Windows服务(使用C#)可以调用无限运行的Python脚本吗?

时间:2018-10-17 14:57:20

标签: c# python windows multithreading windows-services

据我了解,当您运行Windows服务时,它会调用“ onStart()”,并说该服务正在任务管理器中“启动”,直到“ onStart()”方法完全运行为止。然后显示“正在运行”。我想创建一个线程来启动Bluetooth Advertising Script(用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();
    }
}

1 个答案:

答案 0 :(得分:0)

虽然可能还有其他问题,但第一个问题是您的服务(可能)运行的本地服务帐户没有位置libdlib-dev

的权限

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

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