title: .net add handler multithread
body: <p>I’m tying myself up in knots here with multithread in vb. 
  I have a working program that I subscribe to 4 events using the add handler address of and point to a function on my main form. I have 4 separate functions that all do exactly the same thing expect for each of the 4 unique addresses for each of the functions. They have an invoke required, call my delegate thingy at the top of the functions, and then all that the function does is spawn a thread of another process and hand off processing to it.</p>
  
  <p>When I use the program and I handle events from one of the subscriptions everything works great. When I start handling 2 it gets a bit slower, then I do 4 and it really clogs up. When I generate each of the subscribed events they all happen at the same time so windows would get 4 requests at the same time and presumably queue them up and work through them. So this is 1 bottleneck in my application. </p>
  
  <p>What I wondered if it was possible to do, was to point the subscribed events (I.e what I do an add handler address of to) at another thread and have the raised events processed asynchronously across several threads, so I’m not Queuing them up on 1 thread. I know it’s possible because I can run 2 applications and handle the events across 2 of them split and that improves the performance, same goes for if I do 4 separate applications. </p>
  
  <p>I know I have all the right bits I just can’t quite piece it together in the right order.</p>
  
  <pre><code>Public Class MainUI
  
  Public Sub start()
      AddHandler MyEvent.EventHasHappened, AddressOf OnthisUI
      AddHandler ThreadedRoutine.Callback, AddressOf HandedBAck
  End Sub
  
  Delegate Sub On_thisUI()
  Public Sub OnthisUI()
  
      If InvokeRequired Then
          'create a pointer to this function
          Dim MyDel As New On_thisUI(AddressOf OnthisUI)
          'call this same function from this thread
          Dim Eventargs() As Object = {sender, e}
          Invoke(MyDel, Eventargs)
          Return
      End If
  
      Dim Handmeoff As New ThreadedRoutine
      Dim newThread As New System.Threading.Thread(AddressOf Handmeoff.handoffexecution)
      newThread.Start()
  
  End Sub
  
  Public Sub HandedBAck()
      ' Update UI in here
  
  End Sub
  
  End Class
  
  Public Class ThreadedRoutine
  Public Shared Event Callback()
  Public Sub handoffexecution()
      ' Do some work in here
      RaiseEvent Callback()
  
  End Sub
  End Class
  </code></pre>
  
  <p>Sorry for the rough example but this pretty much highlights what i am currently doing</p>
  
  <p>The myevent.eventhashappened is the subscription that i would like to be doing concurrently across several threads rather than using the mainUI thread to receive the events and punt them out to other program threads as they are received.</p>
  
  <p>I thought about putting the Addhandler subscription in another class and just calling instances of that like this</p>
  
  <pre><code>Public Class Subscribe
  Public Sub New()
      AddHandler myevent.eventhashappened, AddressOf HandleMyEvent
  End Sub
  
  Public Sub HandleMyEvent()
  
  End Sub 
  End Class
  </code></pre>
  
  <p>Then i could just declare the 4 listener classes like this</p>
  
  <pre><code>Dim MyHandler1 as new Subscribe
  Dim MyHandler2 as new Subscribe
  Dim MyHandler3 as new Subscribe
  Dim MyHandler4 as new Subscribe
  </code></pre>
  
  <p>Would that work?</p>
  
  <p>Just to try and clear up what I’m trying to show happening here</p>
  
  <p>The myevent.eventhappened is the hook I add at the start. An external event raises this periodically (not shown in the code for clarity at present)</p>
  
  <p>This then triggers a routine in the ui which spawns a thread of a
  New process that handles the event, does some processing (also not shown), starts the thread and when the thread is done it calls back to the ui with some parameters to update ( again not shown)</p>
  
  <p>My wished behaviour is to have that initial hook be initiated and also handled on another thread that is not the UI. I always have to return to UI at some point but I wanted to clear the first bottleneck in the pipeline. </p>
  
  <p>Regards </p>