API 2.0: Thread in Code Question

I’m a little confused right now. I create a thread in my code and assign the run event to it via AddHandler. That works, too. But I fail when adding the UserInterfaceUpdate event. Can someone please modify my code so that I can call AddUserInterfaceUpdate within the ThreadHandler method?

t = New Thread AddHandler t.Run, WeakAddressOf ThreadHandler // How to handle the Thread.UserInterfaceUpdate-Event? t.Run
Thank you.

Did you get that error: http://documentation.xojo.com/api/language/thread.htmlAccessingUIException ?

You’ll want something like:

t = new Thread
AddHandler t.UserInterfaceUpdate,  WeakAddressOf updateUI   // Wire in the thread's UI updater function
Addhandler t.run,                  AddressOf mythreadMain   // Wire in the main function for this thread
t.Start ()                                                  // Everything ready, can run the thread now

In updateUI (), you’ll need to pass it the parameters you need it to have for updating different bits of the UI. I tend to handle each as a case entry in a select case.

I made a UIupdate method to call in the thread when I want to do a specific bit of UI-updating, like this:

UIupdate (action as integer, optional arg1 as variant, ...)

// Stores the requested UI update and sends it off for actioning.

Var  UIdict As new Dictionary

UIdict.Value("action") = action
UIdict.Value("arg1")   = arg1
UIdict.Value("arg2")   = arg2
UIdict.Value("arg3")   = arg3
UIdict.Value("arg4")   = arg4

me.AddUserInterfaceUpdate (UIdict)

You call that in the thread with t.UIupdate (someAction, arg1, etc)

Then in updateUI, the event handler, you have action to select-case on and up to four args for specific cases.

Nope Emile. I use API 2.0 Thread. Combined with Thread.UserInterfaceUpdate there will be no more ThreadAccessingUIExceptions.

Thanks Tim, I’ll try your code later at home :wink:

OK, I got it. I forgot the Thread parameter in my UserInterfaceUpdate event. Thanks, everybody.

AddHandler t.UserInterfaceUpdate, WeakAddressOf UserInterfaceUpdate

Private Sub UserInterfaceUpdate(sender As Thread, data() As Dictionary) #Pragma Unused sender For Each update As Dictionary In data If update.HasKey("UIProgress") Then RaiseEvent Status(update.Value("UIProgress").StringValue) End If Next End Sub