取消后台工作者

发布于 2024-10-02 18:31:58 字数 1451 浏览 7 评论 0原文

我有以下问题,我希望有人能够帮助我解决。

我有一个 VB .net (2010) 工作人员,它运行一个 shell 程序。

shell 程序是一种服务和输出内容,例如:

Server initializing...
Server opening port...
more info...

我能够“捕获”shell 的输出并将其添加到文本框(使用设置文本函数)。

我可以通过单击停止按钮来取消工作程序,但是当 shell 不再输出时,我无法再停止工作程序。

至少我怀疑是这样的。

我尝试检查 endofstream (评论部分),但这不起作用。

我还尝试使用相同的代码与一些测试文本代替“clsProcess.StandardOutput.ReadLine”,这也有效。

所以我得出的结论是它一定与最后的 clsProcess.StandardOutput.ReadLine 有关???

    Try
        clsProcess.StartInfo.UseShellExecute = False
        clsProcess.StartInfo.RedirectStandardOutput = True
        clsProcess.StartInfo.RedirectStandardError = True
        clsProcess.StartInfo.FileName = serverpath + config_executable
        clsProcess.StartInfo.CreateNoWindow = True
        clsProcess.Start()
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "Error starting server")
        Debug.Print(ex.Message)
    End Try

    Do While Not workerServer.CancellationPending
        Try
            'If Not clsProcess.StandardOutput.EndOfStream Then
            SetText(clsProcess.StandardOutput.ReadLine + vbNewLine)
            'End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error adding line to log")
        End Try

        Threading.Thread.Sleep(100)
    Loop

    clsProcess.Kill()

有什么想法吗?

提前致谢!

问候,

PH

I have the following problem and I hope someone will be able to help me with it.

I have a worker in VB .net (2010) which runs a shell-program.

The shell program is a service and output stuff like:

Server initializing...
Server opening port...
more info...

I am able to 'catch' the output of the shell and add it to a textbox (using set text function).

And I am able to Cancel the worker by clicking on a stopbutton, however when there is no more output by the shell I cannot stop the worker anymore.

At least i suspect that's the case.

I've tried checking for endofstream (commented section) but that doesn't work.

I've also tried to same code with some test text in stead of 'clsProcess.StandardOutput.ReadLine' and that also works.

So I came to the conclusion it must have something to do with clsProcess.StandardOutput.ReadLine being at the end???

    Try
        clsProcess.StartInfo.UseShellExecute = False
        clsProcess.StartInfo.RedirectStandardOutput = True
        clsProcess.StartInfo.RedirectStandardError = True
        clsProcess.StartInfo.FileName = serverpath + config_executable
        clsProcess.StartInfo.CreateNoWindow = True
        clsProcess.Start()
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "Error starting server")
        Debug.Print(ex.Message)
    End Try

    Do While Not workerServer.CancellationPending
        Try
            'If Not clsProcess.StandardOutput.EndOfStream Then
            SetText(clsProcess.StandardOutput.ReadLine + vbNewLine)
            'End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error adding line to log")
        End Try

        Threading.Thread.Sleep(100)
    Loop

    clsProcess.Kill()

Any ideas?

Thanks in advance!

Regards,

PH

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Spring初心 2024-10-09 18:31:58

据推测这是在另一个线程上发生的。尝试从 GUI 线程中 Kill() 处理进程,而不是仅仅设置 CancellationPending。您是正确的,ReadLine() 调用是阻塞的,导致 while 循环在没有更多输出时永远不会重新评估其条件。

从另一个线程终止该进程应该可以。 (它可能会从 ReadLine() 抛出异常,因此请做好准备。)

Presumably this is happening on another thread. Try Kill()ing the process from the GUI thread instead of just setting CancellationPending. You are correct that the ReadLine() call is blocking, causing the while loop to never re-evaluate its condition once there is no more output.

Killing the process from another thread should work. (It may throw an exception from ReadLine(), so be prepared for that.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文