Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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:

 

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?

Posted
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.

Posted
thx' date=' application.run does the trick[/quote']

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.

  • 1 month later...
Posted
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!
  • Leaders
Posted
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.
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...