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 来处理 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

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
4

1 回答 1

0

最后,我按照此处标记答案的步骤解决了这个问题:Handle events in another class

于 2013-10-08T05:55:49.277 回答