In your loop, you reach a point where you have (say) a string that you want to put into a listbox cell. Instead of doing that, you will be sending that data (row, column, string) off to be separately added to the listbox.
Yiou want to make a subclass of Thread, and you could add a method to it:
UIupdate (arg1 As variant, arg2 As Variant, arg3 As Variant)
// Stores the requested UI update and sends it off for actioning.
Var UIdict As new Dictionary
UIdict.Value("row") = arg1
UIdict.Value("col") = arg2
UIdict.Value("str") = arg3
me.AddUserInterfaceUpdate (UIdict)
Start your thread thus:
myThread = new mythreadClass ()
AddHandler myThread.UserInterfaceUpdate, WeakAddressOf app.updateUI // Wire in the UI updater function for this thread
Addhandler myThread.run, AddressOf mythreadcode // Wire in the main function for this thread
myThread.Start () // Everything ready, can run the thread now
// Thread goes off and starts doing your loops.
Then in your thread, when you need to add an update to a cell, you do:
me.UIupdate (row, column, string)
Now, where is the actual updating work done? Well, add the mthod to do it - you’ve already seen it referred to as updateUI() which got wired in above as the event handler for user interface updates. This could look like:
updateUI (t As mythreadClass, UIdata() As Dictionary)
Var updateItem as new Dictionary, row, col as integer
while (UIdata.LastRowIndex>-1)
updateItem = UIdata(0)
UIdata.RemoveRowAt (0)
row = updateItem .value("row")
col = updateItem.value("col")
mylistbox.cellValueAt(row, col) = updateItem.value("str")
wend
That’s the outline, showing the overall structure. I’ve not handled adding the row to the listbox, you may wish to add a flag to the list of args to tell the uiupdater to do that, or perhaps extend the argument list to pass all the row/col data at once.
The business with the while loop in the updater handles the case where you’ve added several items to the dictionary array before the updater had a chance to take them off. It will then handle all the queued requests at once.
Note also that you have to add the new listbox row to the listbox in the updater - you can’t do that in the thread. Also, make the updater be a method of the application. That way the thread finds it more easily. And some variables such as the listbox will need to be global or at least with a wide enough scope that they can be referenced.