Solved

VB.NET Loading Dialog crashes form

Posted on 2010-09-22
3
433 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

688 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