I had a similar issue and this post helped me solve the same problem, I will share the specifics of the solution as I understand them.
My System.Process event-listening function triggered the "can only be called from the main thread" issue when I tried to call the delegate function.
private static void RunningProcess_OutputDataReceived (object sender, DataReceivedEventArgs e) {
if(dataCallback != null) {
dataCallback(e.Data);
}
}
So instead of calling it from that function, I assign it to a static variable:
private static void RunningProcess_OutputDataReceived (object sender, DataReceivedEventArgs e) {
if(dataCallback != null) {
outputDataToSend = e.Data;
}
}
And then call the delegate from Update():
void Update () {
if(!string.IsNullOrEmpty(outputDataToSend)) {
dataCallback (outputDataToSend);
outputDataToSend = null;
}
}
This solution is not reliable as the above one is, because if the OutputDataReceived function gets called multiple times before Update, only the most recent data packet will be forwarded. Enqueuing the incoming callbacks as UpTheCreek did is a good way around that, but this solution is very simple and lets you get past the fundamental issue.