Developer, Former MVP, now at Microsoft - Best of 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
Private mDataQue As New Queue Private mUpdateDel As New EventHandler(AddressOf UpdateBox) ' This method runs on a non-GUI thread e.g. Threading.Timer Friend Sub OnNonGuiThread(ByVal o As Object) ' if you have more than one argument ' create an object or structure ' that holds the data you want to pass to the GUI thread SyncLock mDataQue.SyncRoot mDataQue.Enqueue(o) End SyncLock ' assuming all this code is in a form Me.Invoke(mUpdateDel) End Sub ' This method runs on GUI thread Private Sub UpdateBox(ByVal sender As Object, ByVal e As EventArgs) Dim o As Object SyncLock mDataQue.SyncRoot If mDataQue.Count > 0 Then o = mDataQue.Dequeue() End If End SyncLock ' TODO use o ' cast o to your object/structure and ' use it to update the GUI End Sub
' NOW, *this* is the method that runs on non-GUI thread ' e.g. Threading.Timer ' The original method is now just a helper Public Sub OnNonGuiThread2(ByVal o As Object) ThreadPool.QueueUserWorkItem(AddressOf OnNonGuiThread,o) End Sub