# VB.NET Multi Threading Question



## shuggans (Aug 20, 2011)

If i use addhandler, is that starting a new thread automatically?
IE:

```
AddHandler RawVidWatchFolder.Created, AddressOf logchange
```
Does it start the sub logchange on its own thread?


----------



## FordGT90Concept (Aug 20, 2011)

No, AddHandler is the synonymous as doing...


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


----------



## shuggans (Aug 20, 2011)

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.


----------



## FordGT90Concept (Aug 20, 2011)

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


----------

