我创建了一个运行新进程(CLI外部应用程序)的类,该应用程序有自己的进度条,因此我从隐藏的进程控制台中选择进度来计算我的类中控制台外的百分比。 / p>
好吧,我想要做的是在类中创建一个事件(或其他事件,事件只是我觉得有用的第一个事件),返回一个包含进度百分比的整数,该百分比是一个整数从0到99)
我已经阅读了有关如何创建自定义事件的信息,但我很遗憾尝试它,我不知道如何在我的类之外检索整数(变量Percentage)以从其他类中处理它,例如我从我的类中运行一个方法,所以我想在默认的“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 Class
中的代码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来处理Progress事件,但实际上我想像其他方式一样处理它,但我不能这样做而且我不知道原因:
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
那么我可以做些什么来解决这两个问题?
更新:
尝试使用@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 Class:
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