如何调用表单并从主表单运行其进度条?

时间:2016-03-30 01:14:49

标签: vb.net multithreading

我正在努力使用这段代码,我想调用一个非常小的表单,只需要一个关于marquee样式的进度条,向用户显示我们正在等待Dim procMethod2 As Process = Process.Start(psiMethod2)块开始的进程完成。

正在按预期调用,显示和终止表单,但是,当进程在后台运行时,它上面的进度条不会移动...

我已经阅读了几篇关于此的文章但是所有这些都是小的用于此...下面的代码是一个很大的功能,我想在其中管理它。

我做错了什么?

谢谢!

                Try
                    Dim psiMethod2 As New ProcessStartInfo
                    psiMethod2.UseShellExecute = False
                    psiMethod2.WorkingDirectory = Path
                    psiMethod2.WindowStyle = ProcessWindowStyle.Hidden
                    psiMethod2.FileName = strWTReportingFullPath 'Full path of WT-Reporting.exe
                    psiMethod2.Arguments = ExecutableArgs 'Arguments to be passed to WT-Reporting.exe

                    'Launch the process in a hidden window
                    Dim procMethod2 As Process = Process.Start(psiMethod2)

                    'Call the form containing the progress bar
                    frmProgressBarWTReporting.Show(Me)

                    'Set up progress bar settings
                    frmProgressBarWTReporting.pbWTReporting.MarqueeAnimationSpeed = 5
                    frmProgressBarWTReporting.pbWTReporting.Style = ProgressBarStyle.Marquee

                    'Awaits the process to terminate
                    procMethod2.WaitForExit()

                    'Close the form once the process is terminated
                    frmProgressBarWTReporting.Close()

                    If chkboxAddToNCBase.Checked = False Then
                        bolToolListsWereMigrated = False
                        Try
                            StartExplorer(Path)
                        Catch ex As Exception
                            MsgBox("Error when opening the export folder in Windows Explorer!", MsgBoxStyle.Critical, "Error!")
                        End Try
                    Else
                        AddAssociateFileIntoNCBase(intProgramIDAsInteger, strToolListFullPath)
                    End If

                Catch ex As Exception
                    MsgBox(ex.ToString)
                    Exit Sub
                End Try

2 个答案:

答案 0 :(得分:1)

您的代码存在的问题是您通过调用procMethod2.WaitForExit()来阻止UI线程。由于UI被阻止,因此无法更新进度条。

WaitForExit有一个过载需要你可以使用的IntegerInteger指定等待进程退出的毫秒数。如果时间用完,则调用返回False,但如果进程退出(或已退出),则返回True。您可以使用Timer定期调用WaitForExit,然后仅在方法返回True时进行响应。这样你就不会阻止UI线程,并且可以进行更新。

以下是您需要尝试的内容:

    Dim psiMethod2 As New ProcessStartInfo
    psiMethod2.UseShellExecute = False
    psiMethod2.WorkingDirectory = Path
    psiMethod2.WindowStyle = ProcessWindowStyle.Hidden
    psiMethod2.FileName = strWTReportingFullPath 'Full path of WT-Reporting.exe
    psiMethod2.Arguments = ExecutableArgs 'Arguments to be passed to WT-Reporting.exe

    'Launch the process in a hidden window
    Dim procMethod2 As Process = Process.Start(psiMethod2)

    Dim timer As New Timer()
    timer.Interval = 100

    Dim handler As EventHandler = Nothing
    handler =
        Sub(o, e)
            If procMethod2.WaitForExit(1) Then

                timer.Stop()
                RemoveHandler timer.Tick, handler
                timer.Dispose()

                frmProgressBarWTReporting.Close()

                If chkboxAddToNCBase.Checked = False Then
                    bolToolListsWereMigrated = False
                    Try
                        StartExplorer(Path)
                    Catch ex As Exception
                        MsgBox("Error when opening the export folder in Windows Explorer!", MsgBoxStyle.Critical, "Error!")
                    End Try
                Else
                    AddAssociateFileIntoNCBase(intProgramIDAsInteger, strToolListFullPath)
                End If
            End If
        End Sub

    AddHandler timer.Tick, handler

    timer.Start()

而且,只需要一个小小的注释,你需要停止执行像Catch ex As Exception这样的捕获,因为它们会隐藏错误并使代码更加错误。

答案 1 :(得分:1)

所以我用一种非常简单的方式解决了这个问题 - A Do While Loop

我调用的表单代码就像这样简单:

  Private Sub frmProgressBarWTReporting_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        pbWTReporting.MarqueeAnimationSpeed = 5
        pbWTReporting.Style = ProgressBarStyle.Marquee
  End Sub

这是子代码:

                '//////////////////////////////////////////////////////////////
                '// Method 1, launch a process at run time passing arguments //
                '//////////////////////////////////////////////////////////////
                Try
                    Dim psiMethod2 As New ProcessStartInfo
                    psiMethod2.UseShellExecute = False
                    psiMethod2.WorkingDirectory = Path
                    psiMethod2.WindowStyle = ProcessWindowStyle.Hidden
                    psiMethod2.FileName = strWTReportingFullPath 'Full path of WT-Reporting.exe
                    psiMethod2.Arguments = ExecutableArgs 'Arguments to be passed to WT-Reporting.exe

                    frmProgressBarWTReporting.Show()
                    Dim procMethod2 As Process = Process.Start(psiMethod2)

                    Do While 1 = 1
                        If procMethod2.HasExited = False Then
                            Application.DoEvents()
                        Else
                            frmProgressBarWTReporting.Close()
                            Exit Do
                        End If
                    Loop

                    If chkboxAddToNCBase.Checked = False Then
                        bolToolListsWereMigrated = False
                        Try
                            StartExplorer(Path)
                        Catch ex As Exception
                            MsgBox("Erro ao abrir a pasta de exportação no Windows Explorer!", MsgBoxStyle.Critical, "Erro ao abrir a pasta de exportação!")
                        End Try
                    Else
                        AddAssociateFileIntoNCBase(intProgramIDAsInteger, strToolListFullPath)
                    End If

                Catch ex As Exception
                    MsgBox(ex.ToString)
                    Exit Sub
                End Try

感谢大家的投入!