2013-10-07 167 views
0

我創建了一個運行新進程的類(一個CLI外部應用程序),該應用程序擁有自己的進度條,因此我從隱藏的進程控制檯中選擇進度來計算控制檯外部的百分比,類。自定義事件問題

嗯,我想要做的是在類中創建一個事件(或其他事件,只是我認爲有用的第一個事件),以返回包含進度百分比的整數數字(百分比是整數從0到99)

我已閱讀有關如何創建自定義事件,但我很迷茫嘗試它,我不知道如何檢索我的類之外的整數(變量百分比)來處理它來自其他類,例如我從我的類運行一個方法,所以我想在默認的「Form1」類中處理進程百分比進度。

這是我的自定義類的代碼:

Public Class CoreConverter 

Public Shared Effects As String = String.Empty ' DSP Effects 

' <summary> 
' Gets or sets the CoreConverter executable path. 
' </summary> 
Public Shared CoreConverter_Location As String = ".\CoreConverter.exe" 
Public Shared CoreConverter As New Process() 

Public Shared CoreConverter_Info As New ProcessStartInfo() With { _ 
       .CreateNoWindow = True, _ 
       .UseShellExecute = False, _ 
       .RedirectStandardOutput = True, _ 
       .RedirectStandardError = True _ 
} 

Public Shared Event Progress As EventHandler 

Protected Sub OnProgress() 
    RaiseEvent Progress(Me, EventArgs.Empty) 
End Sub 

Public Shared Sub Run_CoreConverter() 

    CoreConverter_Info.FileName = CoreConverter_Location 
    CoreConverter_Info.StandardErrorEncoding = System.Text.Encoding.Unicode 
    CoreConverter_Info.StandardOutputEncoding = System.Text.Encoding.Unicode 
    CoreConverter.StartInfo = CoreConverter_Info 
    CoreConverter.Start() 

    Dim Percentage As Integer = 0 

    While Not CoreConverter.HasExited 

     If ChrW(CoreConverter.StandardOutput.Read) = "*" Then 
      Percentage += 1 
      RaiseEvent Progress(Nothing, EventArgs.Empty) 
     End If 

    End While 

    ' MsgBox("end") 

End Sub 

End Class 

這是Form1類代碼

Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown 
    AddHandler CoreConverter.Progress, AddressOf Process_Progress 
    CoreConverter.Run_CoreConverter(Arguments, blah blah blah) 
End Sub 

Protected Sub Process_Progress(sender As Object, e As EventArgs) 
    ' MsgBox(e.ToString) ' I want to obtain the Integer percentage but don't know how to 
End Sub 

請注意,我需要使用AddHandler的處理進展情況,但實際上我想處理一下這樣的方式別人,但我不能這樣做,我不知道爲什麼:

Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown 
    CoreConverter.Run_CoreConverter(Arguments, blah blah blah) 
End Sub 

Protected Sub Process_Progress(sender As Object, e As EventArgs) _ 
Handles CoreConverter.Progress 
    ' MsgBox(e.ToString) ' I want to obtain the Integer percentage but don't know how to 
End Sub 

所以我能做些什麼來解決這兩個問題?

UPDATE:

嘗試使用@varocarbas解決方案來解決它......

CoreConverter類:

Public Shared WithEvents p_progressBar As ProgressBar 

Public Shared Sub Run_CoreConverter() 

    p_progressBar = New ProgressBar() With {.Maximum = 60} 

    CoreConverter_Info.FileName = CoreConverter_Location 
    CoreConverter.StartInfo = CoreConverter_Info 
    CoreConverter.Start() 

    While Not CoreConverter.HasExited 

     If ChrW(CoreConverter.StandardOutput.Read) = "*" Then 
      p_progressBar.PerformStep() 
     End If 

    End While 

    p_progressBar.Dispose() 
    CoreConverter.Close 

End Sub 

Form1類:

Public Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown 

    ' Exception: p_progressBar is not an event (of course, I know) 
    AddHandler CoreConverter.p_progressBar, AddressOf Process_Progress 

    CoreConverter.Run_CoreConverter(arguments, blah blah blah) 

End Sub 

Sub Process_Progress(sender As Object, e As EventArgs) ' Handles Coreconverter.p_progressBar obviouslly I can't do this... :(
    MsgBox(sender.value) 
End Sub 
+0

我認爲有一個事件(甚至多於一個)佔了progressBar的值變化(大多數控件的通常過程),但令人驚訝的是,ProgressBar並非如此,這就是爲什麼我刪除了我的答案(它的要點恰恰是避免定製任何東西,只依賴內置的功能)。 – varocarbas

+0

@Varocabras只是我只會說你真的非常感謝你的幫助和作出很好的答案。也許會在下一次我可以標記你的答案給你一些觀點:P。 (我很放鬆,原諒我的英語) – ElektroStudios

+0

?!你已經給了我分數(+1),我已經刪除了我的答案。實際上,我可能會編輯我的答案,或者用您正在查找的自定義事件創建一個新答案,並獲得您正在談論的觀點,但感覺不像。對於我們中的一些人來說,積分不是太重要(儘管我傾向於吸引相當多的人:)),但要做正確的事情並幫助合適的人。例如,你在一段時間內不會得到我的進一步幫助,只是因爲今天你已經做了很好的工作來獲得這個獎項;) – varocarbas

回答