Windows服务停止而不是继续运行 [英] Windows service stops rather than keeping running

查看:76
本文介绍了Windows服务停止而不是继续运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经写了一个Windows服务. "Sub OnStart(ByVal args()
"会调用永久运行的第二个子例程.简单地说,当sub结束时,它将再次调用其自身.问题是,当onstart子结束时,服务将无法成功运行.

只是需要注意一点,它会在再次调用自身之前进行线程睡眠,因此不会被锤击.

例如

Hi
I''ve written a windows service. The "Sub OnStart(ByVal args()
" calls a second subroutine which runs perpetually. Simply put when then sub gets to the end it calls its self again. The problem is when the onstart sub comes to an end the service does not keep running successfully.

Just an little additional note it does do a thread sleep before calling its self again so it doesn''t get hammered.

e.g.

Protected Overrides Sub OnStart(ByVal args() As String)

        run_all_my_code()

    End Sub

    Sub run_all_my_code()

        'do a whole lot of stuff

        run_all_my_code()

    End Sub



我得到的一些建议是将进程拆分到单独的线程中

例如



Some advice I got was to split the process onto a separate thread

e.g.

Protected Overrides Sub OnStart(ByVal args() As String)

        Dim t1 As Thread = New Thread(AddressOf run_all_my_code)
        t1.Start()

    End Sub



它似乎工作正常,但我的问题是这是否是最好的方法?这是传统的吗? .Net支持多线程之前,开发人员在做什么?

任何反馈表示赞赏.

M:)



Its seems to work ok but my question is if this is the best way of doing it? Is it conventional? What were developers doing before .Net supported multi threading?

Any feedback appreciated.

M:)

推荐答案

没有没有线程的Window Service之类的东西(病理情况除外). .NET没有线程?从来没有听说过这种奇怪的事情...

基本上,您在正确的道路上.根据您正在执行的操作,您可能需要更多线程.例如,典型的基于TCP的服务将需要一个线程来接受新连接,而其他线程需要在远程套接字的网络流上进行读/写操作.您可能需要一个线程来协调其他线程.除此之外,您还必须详细说明OnStop的情况,以便所有线程都可以安全地退出或中止(关闭所有文件句柄/流并完成所有其他死后操作而没有死锁).

—SA
There is no such thing as Window Service without thread (except pathological cases). .NET without threads? Never heard of such weird thing...

Basically, you''re on right path. Depending on what you''re doing, you may need more threads. For example, a typical TCP-based service will need a thread to accept new connections, and other thread doing read/write on the networks streams of the remote sockets. You may need a thread coordinating other threads. In addition to that, you have to elaborate the scenario for OnStop, so all threads could exit or abort safely (closing all file handles/stream and completing all other post-mortal actions without dead-locking).

—SA


这篇关于Windows服务停止而不是继续运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆