SgDotNet
Singapore Professional .NET User Group -For Cool Developers

Create and open a Windows Form in new Thread

rated by 0 users
This post has 1 Reply | 2 Followers

Top 25 Contributor
Posts 176
choongseng Posted: 03-03-2007 12:35 PM

I've a main form and upon pressing of a button, wanted to create a new form in another thread.

However, when the new form is launched in the new thread, it seems that it simply disappear thereafter.

This happens even if the thread is being keep tracked (thread not being disposed).

Give it a try and let me know if you have any idea on a solution to it.

Thanks.

regards,

choongseng 

You can try the codes below:

    Private Sub btnForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForm.Click
        LaunchForm()
    End Sub

    Private Sub btnThreadForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThreadForm.Click
        Dim th As New Threading.Thread(AddressOf LaunchForm)
        th.Start()
    End Sub

    Private Sub LaunchForm()
        Dim frm As New frmThreadDemo
        frm.Show()
    End Sub

my development sketch book my personal blog
Top 10 Contributor
Posts 2,284

Consider the life span of your thread - LaunchForm() is only two lines for the new thread to execute. What happens after that? It terminates; nothing else to do.

Since that thread is the owner of the new form (having created it), it disappears along with the thread.

Typically there is only one UI thread that renders all the UI elements. Additional threads are worker threads that do work not pertaining to drawing the UI. If you really want another thread to handle UI-rendering chores of new windows, you must then keep that thread "busy" so it does not terminate.

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Page 1 of 1 (2 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems