我创建了一个类,在其中运行一个新进程(CLI 外部应用程序(,该应用程序有自己的进度条,所以我从隐藏的进程控制台中选择进度以计算控制台外的百分比,在我的类中。
好吧,我想做的是在类中创建一个事件(或其他什么,事件只是我认为有用的第一个事件(以返回一个包含进度百分比的整数(百分比是从 0 到 99 的整数(
我已经阅读了有关如何创建自定义事件的信息,但我在尝试它时很迷茫,我不知道如何在我的类之外检索整数(变量 Percent(以从其他类处理它,例如我从我的类运行一个方法,所以我想处理默认"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 来处理 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解决方案来解决它...
核心转换器类:
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
中一类:
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
我按照此处标记答案的步骤解决了这个问题: 处理另一个类中的事件