【问题标题】:Is there a way to extend splash screen into form shown event?有没有办法将启动画面扩展到表单显示事件?
【发布时间】:2014-10-22 14:27:39
【问题描述】:

我最近正在处理这个错误:BeginInvokeStackflowError

我正在使用线程,根据我的研究,这是因为在线程 .start() 事件中它调用了 .invoke。如果这是在 mainform_Load 事件中完成的,那么在它准备好之前,您会收到 BeginInvoke 错误。

所以我已经将我的代码从加载移动到显示的事件。但是,在后台发生了很多我不希望用户看到的事情。我的代码中有没有办法扩展启动画面我必须等到显示的主窗口第一次完成?

 Private Sub MainWindow_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    'update table /search network       
    updateTable()
    'clean 
    cleanupTable()
    'fix label 
    updateLabel()
End Sub

【问题讨论】:

  • 在具有类 UI 的代码中或在加载/显示表单之前(在类中)执行您不希望他们看到的内容
  • 嗯,我移动到所示事件的线程内容是收集值并为我在数据表中清理的 datagridview 创建列。列尚未创建,因此必须在完成之后。

标签: vb.net splash-screen


【解决方案1】:

除了 VB Application Framework 提供的默认“MainForm”方法之外,您的应用程序也可以启动。这将使用Sub Main 作为起点,让您可以控制显示的表单和时间,以及在此之前会发生什么:

' IF your form is declared here, it will be
' available to everything. e.g.:
' Friend mainfrm As Form1

Public Sub Main()
    ' this must be done before any forms/controls are referenced
    Application.EnableVisualStyles()

    ' the main form for the app
    ' just "mainfrm = New Form1" if declared above
    Dim mainfrm As New Form1

    ' eye candy for the user
    Dim splash As New SplashScreen1
    splash.Show()

    ' your code here to do stuff.
    ' you can also invoke your procedures on the main form
    ' mainfrm.FirstTimeSetup()

    ' for demo purposes
    System.Threading.Thread.Sleep(2500)

    ' close/hide the splash once you are done
    splash.Close()
    ' see note

    ' start the app with the main form
    Application.Run(mainfrm)

End Sub
  • 向项目中添加一个模块,通常是“程序”
  • 添加您的子主目录
  • 转到项目属性,取消选中使用应用程序框架
  • 在 StartUp 对象下拉列表中选择 Sub Main

如果您在顶部声明初始屏幕为朋友,您可以真正扩展它,直到所有表单的加载事件完成并在那里/然后关闭它。

【讨论】:

  • 感谢详细的解释!我现在正在尝试。
  • 我试过了,解决了一些问题。我认为我需要弄清楚的唯一一件事是如何让我在屏幕上加载的 gif 在图片框中再次动画(imageanimator 不工作,嗯)。否则,工作得很好。谢谢
  • 如果你有一个实际的动画 GIF,图片框会自动为它制作动画。
【解决方案2】:

如果您使用项目设置实现了初始屏幕,则无法扩展它,但是您可以将初始屏幕表单用作初始表单而不是主表单。至于等到线程完成显示表单(或隐藏启动屏幕),请考虑在主表单中使用公共布尔值,并在线程完成后将其更改为 True。您可以在初始屏幕上使用计时器来检查此布尔值更改,然后将表单的不透明度更改回 1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    相关资源
    最近更新 更多