2010-11-15 126 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