Solved

VB.NET Loading Dialog crashes form

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
start a process from a service 3 36
i have to take the screenshot of command prompt? how to do this? 1 58
ModalPopup  question 22 36
"lblTime is not declared" 3 27
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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