Solved

VB.NET Loading Dialog crashes form

Posted on 2010-09-22
3
429 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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