Solved

VB.NET Loading Dialog crashes form

Posted on 2010-09-22
3
423 Views
Last Modified: 2012-05-10
Hi,

I have an application with a main form. On this page when a user click certain labels etc it will show another form, eg Form1. Form1 takes about 20 seconds to load the data in etc, so I have been trying to find a loading dialog. I have found one that is sort of working, but for some reason it crashes the just opened form. Here is the code for the loading form;

Imports System.ComponentModel
Public Class Loading
    Private _worker As BackgroundWorker
    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
    Private Sub WorkerDoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
        Form1.Show()
    End Sub
    Private Sub WorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub
End Class

Open in new window



On the main page, I have the following code in the label;

Dim frm As New Loading()
frm.ShowDialog()

Open in new window


Now, when I click the Label on the main form, The loading box comes up and works fine. Once the data has finished loading the box disappears, as it should and leaves me with Form1. However Form1 will just hang and crash without any errors after it has been loaded.

Could anyone please help with this code?

Thank you kindly
0
Comment
Question by:DanielBlackmore
3 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 250 total points
ID: 33733651
The reason is that Form1 has been started from the Loading thread. As the loading thread has finished, Form1 is orphan.

The best solution is that the Loading thread must not open Form1. Form1 must be open from the main form. First, open the Loading thread (that I suppose that shows any kind of "loading" message), and after that open Form1. As the Loading thread executes in it's own thread space, it will show the message anyway.

Hope that helps.
0
 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 250 total points
ID: 33733725
You open the Form on a different thread so there is no message queue associated with that form. That is the reason your form  hangs around not responding. You will need to change your layout logic so that the form uses the original UI thread with the message queue.
 
0
 

Author Closing Comment

by:DanielBlackmore
ID: 33769978
Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now