取消后台工作者
我有以下问题,我希望有人能够帮助我解决。
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据推测这是在另一个线程上发生的。尝试从 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 settingCancellationPending
. You are correct that theReadLine()
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.)