Attaching a thread

  • Thread starter Thread starter Adam Honek
  • Start date Start date
A

Adam Honek

How does one attach a thread so it can update the UI of a form?

The other thing is I thought .IsBackground makes the thread active so it
doesn't stop looping until the main thread dies. This doesn't seem to be the
case however with it just dying after one run.

I recall there being a Win32 API by the name AttachThreadInput, is this the
answer?

I'm using the code below to launch the thread.

Dim MyBackgroundThread As System.Threading.Thread

MyBackgroundThread = New System.Threading.Thread(AddressOf
mybackgroundcheck)

MyBackgroundThread.SetApartmentState(Threading.ApartmentState.STA)

MyBackgroundThread.IsBackground = True

MyBackgroundThread.Start()

Thanks,

Adam
 
Adam Honek said:
How does one attach a thread so it can update the UI of a form?

'Control.{InvokeRequired, BeginInvoke, Invoke}':

Multithreading in Windows Forms applications
The other thing is I thought .IsBackground makes the thread active so it
doesn't stop looping until the main thread dies. This doesn't seem to be
the case however with it just dying after one run.

'IsBackground' will cause the thread to be killed when the main thread it is
belonging to terminates. You'll add additional logic such as a loop or
'Thread.Sleep' to your thread in order to prevent it from terminating.
 
1) MS has changed .resume and .suspend in .NET 2.0 but I can't
find how it's done now.

2) I can't see any info there regarding how to update a form in a different
thread. It seems a global variable must be used.

Still need to check if possibly any .begininvoke might do it.

Adam
 
Hello Adam,
1) Resume and Suspend methods are still there, although you shouldn't use them: it's recommended the use of AutoResetEvent or other synchronization mechanisms, so that your thread decides when it can become suspended.
2)Herfried has provided you with a link for that purpose. You use Invoke or BeginInvoke methods if you want to interact with the user interface. Anyway don't hesitate to use a global variable if you need it.

Regards.


"Adam Honek" <[email protected]> escribió en el mensaje | 1) MS has changed .resume and .suspend in .NET 2.0 but I can't
| find how it's done now.
|
| 2) I can't see any info there regarding how to update a form in a different
| thread. It seems a global variable must be used.
|
| Still need to check if possibly any .begininvoke might do it.
|
| Adam
|
|
| | >> How does one attach a thread so it can update the UI of a form?
| >
| > 'Control.{InvokeRequired, BeginInvoke, Invoke}':
| >
| > Multithreading in Windows Forms applications
| > <URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithreading&lang=en>
| >
| >> The other thing is I thought .IsBackground makes the thread active so it
| >> doesn't stop looping until the main thread dies. This doesn't seem to be
| >> the case however with it just dying after one run.
| >
| > 'IsBackground' will cause the thread to be killed when the main thread it
| > is belonging to terminates. You'll add additional logic such as a loop or
| > 'Thread.Sleep' to your thread in order to prevent it from terminating.
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
 
Back
Top