C
C#2w ago
stigzler

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:
internal class SystemEventsHook : ISystemEventsPlugin
{
private BigBoxViewController BigBoxViewController;

[STAThread]
public void OnEventRaised(string eventType)
{
switch (eventType)
{
case SystemEventTypes.PluginInitialized:
Initialization.InitialiseGearbox();
break;

case SystemEventTypes.BigBoxStartupCompleted:

Thread bigBoxThread = new Thread(new ThreadStart(() =>
{
BigBoxViewController = new BigBoxViewController(this);
BigBoxViewController.OnBigBoxStartupCompleted();
System.Windows.Threading.Dispatcher.Run();

}));
bigBoxThread.SetApartmentState(ApartmentState.STA);
bigBoxThread.IsBackground = true;
bigBoxThread.Name = "BigBoxViewController";
bigBoxThread.Start();
break;
}

if (BigBoxViewController != null)
{
BigBoxViewController.ProcessSystemEvent(eventType);
}
}
}
internal class SystemEventsHook : ISystemEventsPlugin
{
private BigBoxViewController BigBoxViewController;

[STAThread]
public void OnEventRaised(string eventType)
{
switch (eventType)
{
case SystemEventTypes.PluginInitialized:
Initialization.InitialiseGearbox();
break;

case SystemEventTypes.BigBoxStartupCompleted:

Thread bigBoxThread = new Thread(new ThreadStart(() =>
{
BigBoxViewController = new BigBoxViewController(this);
BigBoxViewController.OnBigBoxStartupCompleted();
System.Windows.Threading.Dispatcher.Run();

}));
bigBoxThread.SetApartmentState(ApartmentState.STA);
bigBoxThread.IsBackground = true;
bigBoxThread.Name = "BigBoxViewController";
bigBoxThread.Start();
break;
}

if (BigBoxViewController != null)
{
BigBoxViewController.ProcessSystemEvent(eventType);
}
}
}
3 Replies
lycian
lycian2w ago
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 started
stigzler
stigzlerOP2w ago
ProcessSystemEvent 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!)
lycian
lycian2w ago
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
Want results from more Discord servers?
Add your server