【问题标题】:Windows Services starting issueWindows 服务启动问题
【发布时间】:2014-08-14 09:03:04
【问题描述】:

我有多线程的 Windows 服务,线程包含应该始终激活的 while 循环,从安装在那里的其他应用程序发送错误的套接字线程,初始化几个计时器的线程 当我启动线程几秒钟,然后抛出异常,表示我的服务停止工作并要求将数据发送给 Microsoft。 我不知道是什么让我的服务做到了,可能是线程,但我不明白我的实现有什么问题 另外我使用自定义安装程序,当用户启动应用程序时,它将安装该服务。 tools 是一个包含静态数学的类。 我的代码:

public partial class MyService : ServiceBase
{

    public static System.Timers.Timer key;
    public static System.Timers.Timer browse;
    public static System.Timers.Timer sender;

    // array of worker threads
    Thread[] workerThreads;

    public MyService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        workerThreads = new Thread[] 
        { 
            new System.Threading.Thread(new ThreadStart(InitTimers)),
            new System.Threading.Thread(new ThreadStart(Tools.CheckApplication)), 
            new System.Threading.Thread(new ThreadStart(Tools.CheckBrowser)), 
            new System.Threading.Thread(new ThreadStart(Tools.SendOnError))
        };

        // start the threads
        for (int i = 0; i < workerThreads.Length; i++)
        {
            workerThreads[i].Start();
        }
    }

    protected override void OnStop()
    {
    }

    public void InitTimers()
    {
        key = new System.Timers.Timer();
        key.Interval = 1;
        key.Elapsed += Other.key_Tick;
        key.Enabled = true;

        browse = new System.Timers.Timer();
        browse.Interval = 1;
        browse.Elapsed += Other.browse_Tick;
        browse.Enabled = true;

        sender = new System.Timers.Timer();
        sender.Interval = 3600000;
        sender.Elapsed += Other.sender_Tick;
        sender.Enabled = true;

        key.Start();
        sender.Start();
        browse.Start();
    }
}

编辑:

如果我找到了正确的日志,那么例外是:

The thread tried to read from or write to a virtual address for which it does not have the appropriate access.

从来没有见过它...从什么异常来,为什么? p.s 日志是假设发送给 Microsoft 的日志,我没有创建事件日志,所以我想我没有

【问题讨论】:

  • 你能检查一下事件日志吗?该服务应该已经创建了一个错误日志,其中可能包含其他信息...您也可以尝试使用管理员用户启动服务来检查是否是权限问题。
  • 您的一个线程失败了...您发布的代码看起来不错...
  • 已编辑:添加异常
  • 注释掉每个新线程,直到您发现哪个线程抛出了错误 - 如果您仍然需要帮助,请发布它的代码。
  • 您可能需要执行stackoverflow.com/a/3372358/292411 中提到的检查(相同的错误消息)

标签: c# windows multithreading service


【解决方案1】:

我从您的代码中猜测您正在访问桌面上的浏览器。

How can I configure my windows service in the code to access the desktop?

阅读两个答案,一个告诉您如何启用访问,另一个强烈建议另一种设计(与服务交互的客户端应用程序)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 2014-04-17
    • 2020-08-25
    • 1970-01-01
    相关资源
    最近更新 更多