- Home /
Delegates/Events across threads
Hey everyone. I've tried looking this up on Google, but no luck. I don't know much about delegates and events besides the basics, so bear with me. Say I start another thread via QueueUserWorkItem(), and that thread calls an event upon completion. How can I have any code listening to that event execute in the main thread instead of the QueueUserWorkItem thread?
For example, this class starts a thread and fires an event from that thread.
using UnityEngine;
using System.Threading;
class SomeClass : Monobehaviour {
public delegate void SomeDelegate();
public event SomeDelegate SomeEvent;
void Start() {
QueueUserWorkItem(new object);
}
void ThreadedProcess(object state) {
//CPU intensive code here
SomeEvent();
}
}
Then this class listens for that event.
using UnityEngine;
class EventListener : Monobehaviour {
void Awake() {
SomeClass.SomeEvent += ExecuteOnEvent;
}
void ExecuteOnEvent() {
//I want this code executed from the main thread
}
}
ExecuteOnEvent() gets called from the QueueUserWorkItem thread. How can I have this method called from the main thread? Any help would be greatly appreciated :)
Your answer
Follow this Question
Related Questions
Can't include System.Threading.Tasks 3 Answers
Is there any event or delegate that is called with OnValidate() ? 0 Answers
Unity Events - Subscribe and Unsubscribe Odd Behavor 0 Answers
Help to understand events and delegates (Game Manager) 2 Answers
Using Delegates for separating UI and Logic, are these in the right place? 1 Answer