2010-11-15 132 views
0

我有以下问题,希望有人能帮助我。取消后台工作人员

我有一个工作在运行shell程序的VB.net(2010)。

shell程序是服务输出的东西,如:

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

我能“抓”外壳的输出,并把它添加到一个文本框(使用设置文本功能)。

,我能够通过点击STOPBUTTON取消了工人,然而,当有由外壳输出没有更多的,我不能停止的工人了。

至少我怀疑是这样。

我试过检查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

+0

你能后的代码在你停止按钮的点击事件?和代码可能调用的任何相关函数? – overslacked 2010-11-15 19:57:01

回答

2

据推测,这是发生在另一个线程。从GUI线程尝试Kill()而不是仅设置CancellationPending。您正确地认为ReadLine()调用被阻止,导致while循环在没有更多输出时不再重新评估其条件。

从另一个线程杀死进程应该工作。 (这可从ReadLine()抛出一个异常,那么请做好准备。)

+0

OMG你摇滚!谢了哥们。很简单。 *耻辱*。我认为这个过程在GUI线程中是无法访问的!再次感谢。 – PeeHaa 2010-11-15 20:06:12

+0

没问题。如果有帮助,请不要忘记接受这个答案。 :) – cdhowie 2010-11-15 20:07:12

+0

完成。想要投票给你,但我不能(没有代表)。只记得我想投票给你什么是值得的:) – PeeHaa 2010-11-15 20:09:01