在“请等待”对话框中封装长时间运行的函数

时间:2015-03-04 09:21:45

标签: .net vb.net

我有一个PleaseWaitDialog,其功能如下

Public Class PleaseWaitDialog

    Private RunFunction As Action(Of PleaseWaitDialog, List(Of Object))
    Private Args As List(Of Object)
    Friend WithEvents BackgroundWorker1 As System.ComponentModel.BackgroundWorker

    Public Sub New(RunFunction As Action(Of PleaseWaitDialog, List(Of Object)), Args As List(Of Object))
        Me.RunFunction = RunFunction
        Me.Args = Args
    End Sub

    Private Sub PleaseWaitDialog_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Me.BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Me.RunFunction.Invoke(Me, Me.Args)
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Me.Close()
    End Sub
End Class

PleaseWaitDialog在其构造函数中为长时间运行的方法获取委托,在显示对话框时运行该方法,然后在方法完成时关闭。

为了使这适用于任何现有的长时间运行方法,我必须使用现有函数并更改其签名以获取PleaseWaitDialog和参数对象列表,而不是具有任何类型安全性方法。 e.g。

Public Sub LongRunningMethod(SomeString As String, SomeInt as Int)
End Sub

必须改为:

Public Sub LongRunningMethod(PleaseWait As PleaseWaitDialog, Args As List(Of Object))
    Dim SomeString As String = Args(0)
    Dim SomeInt as Integer = Args(1)
End Sub

我更愿意找到一些方法来保持这些长期运行方法的类型安全。

有什么建议吗?

0 个答案:

没有答案