【问题标题】:Progress bar, wait form进度条,等待表单
【发布时间】:2015-08-14 02:24:54
【问题描述】:

在我的主窗体中,我有一个按钮可以跨多个数据库加载大量存储过程。我想在加载时创建一个进度/等待表单。当我按下按钮时,它会显示等待表单。
我有一个调用workerDoWork 的后台工作进程。 在该方法中,我想调用具有所有代码的主窗体来进行加载。当我公开它时,我可以调用它。但是主窗体内有很多引用,全局变量,被调用的不同方法都不起作用。我怎样才能让它工作而不必将所有代码移动到等待表单中。?

等待表单中的代码。

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)

    _worker = New BackgroundWorker()
    AddHandler _worker.DoWork, AddressOf WorkerDoWork
    AddHandler _worker.RunWorkerCompleted, AddressOf WorkerCompleted

    _worker.RunWorkerAsync()
End Sub

' This is executed on a worker thread and will not make the dialog unresponsive.  If you want
' to interact with the dialog (like changing a progress bar or label), you need to use the
' worker's ReportProgress() method (see documentation for details)
Private Sub WorkerDoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
    'Dim main As New frmMain()
    frmMain.LoadARData()
    ' MsgBox("hi there ")

End Sub




' This is executed on the UI thread after the work is complete.  It's a good place to either
' close the dialog or indicate that the initialization is complete.  It's safe to work with
' controls from this event.
Private Sub WorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
    Me.DialogResult = Windows.Forms.DialogResult.OK
    Me.Close()
End Sub

【问题讨论】:

  • 请向我们展示您的代码,否则我们无法为您提供帮助!
  • 它以什么方式“不起作用”?
  • 好吧,我发现它很奇怪..如果我从主窗体运行方法,我将一个日期变量传递给我的存储过程。 dtpAgeDte.Text 在加载时自动设置。但是如果我从等待表单中调用该方法..它会爆炸并说不能将字符串“”转换为日期..就像它没有拿起日期
  • 如果您使用 BGWm 进行处理,为什么还需要等待表单?
  • 我只有一个显示“等待”的表单,一个向用户显示它正在加载的加载表单......这就是重点

标签: .net vb.net


【解决方案1】:

你有:

frmMain.LoadARData()

正在使用 frmMain 的 默认实例,很可能与您在屏幕上显示的不同(导致错误和意外行为)。

您需要引用正确的 frmMain 实例才能使其正常工作。解决此问题的一种方法是将 frmMain 的引用传递给 Show()/ShowDialog() 调用:

' ... running from within frmMain ...
Dim progress As New frmProgress()
progress.ShowDialog(Me) ' <-- passing in "Me" (the reference to frmMain)

现在,在您的进度表单中,您可以将 Owner 属性转换为 frmMain 类型并调用您想要的方法:

Private Sub WorkerDoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
    Dim main As frmMain = DirectCast(Me.Owner, frmMain)
    main.LoadARData()
End Sub

【讨论】:

猜你喜欢
  • 2022-01-21
  • 1970-01-01
  • 2010-11-16
  • 1970-01-01
  • 2020-01-12
  • 1970-01-01
  • 2019-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多