running form async

natarius

Newcomer
Joined
Oct 31, 2005
Messages
11
hi,
i would like to know how to run a complete form with its controls in a seperate thread as my main thread!?

i have test the following, but it does not work:

Code:
Imports System.Threading
Public Class Form1
    Dim thread As Thread
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        thread = New Thread(AddressOf startform)
        thread.IsBackground = True
        thread.Start()
    End Sub

    Private Sub startform()
        Dim newform As New Form2
        newform.Show()
    End Sub
End Class

it only works like this:

Code:
Imports System.Threading
Public Class Form1
    Dim thread As Thread
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        thread = New Thread(AddressOf startform)
        thread.IsBackground = True
        thread.Start()
    End Sub

    Private Sub startform()
        Dim newform As New Form2
        newform.Show()

        do
            Application.DoEvents()
        loop
        
    End Sub
End Class

so i simulate application.run with a doevents loop...is that ok, or is there a better way?
 
natarius said:
hi,
i would like to know how to run a complete form with its controls in a seperate thread as my main thread!?
What for? One thread is much the same as another so why do you need to have a UI running specifically on the non-start thread?

i have test the following, but it does not work:
...snip...

Yeah, hardly surprising really. "thread.IsBackground = True" sets your form to run on a background thread, a process terminiates when all non-background threads terminate. So you start put a ui on a background thread and the main thread will end taking the entire app with it.

First of all to actually run the form you need to set up the thread to hold the messageloop, DoEvents will cause pumping of messages but a good rule of thumb is that if you ever find yourself using DoEvents you should work out why you need to do so and remove it. Its no substitute for a proper implementation of waiting or callbacks. You want to call Application.Run or create your own ApplicationContext to handle the messageloop, i suggest Application.Run.

Take out the background setting on the thread that the ui will attach to and put an Application.Run in there to get it working.
 
natarius said:
thx, application.run does the trick
I'd still like to know exactly why you're trying this though, there may well be a better/more appropriate way to solve your problem.
 
because i am developing a multimonitor touchscreen application which needs to be responsive all the time! so when for example i fill a listview with images on one form/screen all the others stay responsive!
 
What you need to do is perform logic on a second thread, instead of running the UI on a second thread. As far as updating UI elements, this must be done on the UI's thread, but can be invoked from another thread using the Control.Invoke or Control.BeginInvoke methods.
 
Back
Top