Calling a method in a class running on a separate thread from the main thread
Hi. I'm not very good at threading stuff!
I'm developing a plugin for a 3rd party app. The app provides hooks via a
ISystemEventsPlugin
interface. Whenever an event happens in the app, it is raised in this interface.
I am wanting to start a class ("process"?) on another thread with its being instantiated by a specific event fired by the app (SystemEventTypes.BigBoxStartupCompleted
). This is only fired once. I have successfully instantiated the desired class on another thread (I think - it runs OK and I can see it as a separate thread in the "Parallel Stacks" debug window). However, my query is how do I pass subsequent SystemEventTypes
firings to this instance.
The way I do it at the moment appears to work functionally, but it doesn't feel right as I'm essentially doing a cross-thread call. Here's my code presently:
3 Replies
By design you'd have to do a cross thread call if you want to process an event on a different thread. It would definitely depend on what
ProcessSystemEvent
does but remember that the method itself will be on the current thread, not the bigBoxThread
you startedProcessSystemEvent
eventually leads to UI operaitons. That's what started all this - am getting and STA thread error. How would I relay messages across threads? I.e. how would i call BigBoxViewController.ProcessSystemEvent cross threaded (I know I'm butchering these terms!)Different systems have different ways of getting to the UI thread. If you have a dispatcher instance you can use the invocation methods on that such as
InvokeAsync