• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

VB.NET Multi Threading Question

Joined
Dec 22, 2007
Messages
184 (0.03/day)
Location
Central Nebraska
System Name CORSAIR
Processor AMD Phenom 1090T x6 @3.2
Motherboard Gigabyte Ga-78LMT-S2P
Cooling High efficiency dust cooling
Memory 8GB GSkill DDR3 1333
Video Card(s) Sapphire Radeon HD3870 512MB GDDR4 PCI-e toxic
Storage Seagate SV35.3 ST3250310SV 250GB SATAII(Windows 7 Pro x64), 320GB Samsung SATAII(Storage and SuSE)
Display(s) Dell 20" LCD, Dell 17" LCD
Power Supply Antec Cool Blue 650W Modular
Software Windows 7 Professional SP1, Visual Studio 2010 Ultimate
If i use addhandler, is that starting a new thread automatically?
IE:
Code:
AddHandler RawVidWatchFolder.Created, AddressOf logchange
Does it start the sub logchange on its own thread?
 
No, AddHandler is the synonymous as doing...

Code:
Private Sub logchanged() [b]Handles RawVidWatcherFolder.Created[/b]
End Sub

AddHandler just does it at run time instead of compile time.

It tells logchange to handle the "Created" event when it is raised via "RaiseEvent." It runs in the executing thread. For example, if the main thread raised the event, logchange will be ran on the main thread. If it was raised by a worker thread, it will be ran on the worker thread.

Events are most frequently used when one thread (e.g. worker thread) has to notify another thread (e.g. GUI thread) of a change.
 
Last edited:
So how do I change the thread the event is run on? I want the handler to run on a new thread each time a new file is detected.
 
Code:
Private Sub StartWorker()
  Dim worker As Thread = New Thread(NameOfWorkerMethod)
  worker.Start()
End Sub

Private Sub NameOfWorkerMethod()
  ' Code to perform in worker thread.
End Sub

I recommend perusing the code attached here:
http://www.techpowerup.com/forums/showpost.php?p=2368732&postcount=29

It has a working async thread with delegates, events, and handling of a cross-thread reference.
 
Back
Top