在 vb.net 中处理来自多个源的事件和通知到主窗体的最佳方法

发布于 2024-12-06 06:13:30 字数 413 浏览 0 评论 0原文

我有一个 vb.net 应用程序,其中有一个主窗体。大约有 10 个类,每个类都有自己的功能,如 tcpactions、fileactions、serialport 操作等。用户与主窗体交互并执行某些操作,这些操作将调用这些类中的方法。现在我在主窗体中有一个通知文本区域,我想在通知区域中显示这些类中正在执行的当前操作及其结果。

例如,当用户单击表单中的“启动进程”按钮时,我会调用“ProcessActions”类中的“launchprocess”方法。现在,此方法尝试启动大约 7 个不同的进程,启动后它会发送诸如“进程 1 启动”之类的通知,或者如果失败,它会发送诸如“进程 1 启动失败”之类的通知。

我目前使用事件处理程序并使用它们来显示通知,但是随着我必须处理的事件数量的增加,它变得越来越麻烦,我将来可能需要添加更多的类。那么有没有更好的方法来处理来自其他类的通知。

I have a vb.net application in which there is a main form. There are around 10 classes each having their own functionalities like tcpactions, fileactions, serialport actions etc. The user interacts with the main form and does certain actions which will invoke the methods in these classes. Now I have a notifications textarea in the mainform and i want to show the current action being performed in those classes and their results in the notifications area.

for example when a user clicks a "Start Process" Button in the form i invoke the method "launchprocess" in a class "ProcessActions". Now this method tries to launch about 7 different process and after launching it sends notification such as "process 1 launched" or if it fails it sends notifications such as "process 1 launch failed".

I currently use event handlers and use them to show notifications but with the amount of events i have to handle it is getting cumbersome and i might have to add even more classes in the future. So is there a better way of handling notifications from other classes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2024-12-13 06:13:30

我建议在主窗体可以响应的所有类中使用通用的自定义事件处理程序。首先定义一个新的 EventArgs 类来处理您发送回的通知:

Public NotInheritable Class NtfyEventArgs
    Inherits EventArgs

    Private _Action As String

    Public ReadOnly Property Action As String
        Get
            Return _Action
        End Get
    End Property

    Public Sub New(ByVal action As String)
        _Action = action
    End Sub
End Class

接下来定义一个基类来引发通知:

Public Class Base
    Public Event Ntfy(ByVal sender As Object, ByVal e As NtfyEventArgs)

    Protected Sub RaiseNtfy(ByVal action As String)
        RaiseEvent Ntfy(Me, New NtfyEventArgs(action))
    End Sub
End Class

让其他每个类都从该基类继承。然后发送通知就变得非常容易:

Public Class ProcessActions
    Inherits Base

    Public Sub LaunchProcess
        'Do stuff
        RaiseNtfy("Process 1 launched")

        'Do more stuff
        RaiseNtfy("Process 2 launched")
    End Sub
End Class

最后在主窗体中,监听子类引发的任何事件

Public Class Form1
    Private WithEvents process As New ProcessActions
    Private WithEvents file As New FileActions

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Add Event handlers
        AddHandler process.Ntfy, AddressOf HandleNtfy
        AddHandler file.Ntfy, AddressOf HandleNtfy
    End Sub

    'One procedure to handle all the incoming notifications
    Private Sub HandleNtfy(ByVal sender as Object, ByVal e as NtfyEventArgs) 
        lblNtfy.Text = e.Action

        'If need to take different actions based on the class sending notification
        If TypeOf(Sender) Is ProcessActions Then
            'Specific code for ProcessActions
        End If
    End Sub
End Class

I would suggest using a common custom event handler in all of the classes that the main form can respond to. First define a new EventArgs class to handle the notifications you're sending back:

Public NotInheritable Class NtfyEventArgs
    Inherits EventArgs

    Private _Action As String

    Public ReadOnly Property Action As String
        Get
            Return _Action
        End Get
    End Property

    Public Sub New(ByVal action As String)
        _Action = action
    End Sub
End Class

Next define a base class to raise the notifications:

Public Class Base
    Public Event Ntfy(ByVal sender As Object, ByVal e As NtfyEventArgs)

    Protected Sub RaiseNtfy(ByVal action As String)
        RaiseEvent Ntfy(Me, New NtfyEventArgs(action))
    End Sub
End Class

Have each of your other classes inherit from the base class. Then it becomes quite easy to send a notification:

Public Class ProcessActions
    Inherits Base

    Public Sub LaunchProcess
        'Do stuff
        RaiseNtfy("Process 1 launched")

        'Do more stuff
        RaiseNtfy("Process 2 launched")
    End Sub
End Class

Finally in your main form, listen for any events raised by the child classes

Public Class Form1
    Private WithEvents process As New ProcessActions
    Private WithEvents file As New FileActions

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Add Event handlers
        AddHandler process.Ntfy, AddressOf HandleNtfy
        AddHandler file.Ntfy, AddressOf HandleNtfy
    End Sub

    'One procedure to handle all the incoming notifications
    Private Sub HandleNtfy(ByVal sender as Object, ByVal e as NtfyEventArgs) 
        lblNtfy.Text = e.Action

        'If need to take different actions based on the class sending notification
        If TypeOf(Sender) Is ProcessActions Then
            'Specific code for ProcessActions
        End If
    End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文