Hi!
I got a problrem with network: after calling Socket.BeginReceive, EndReceive seems to operate in a different thread, and any exception which may occur there is surely crashing the editor. I have not tested that with standalone builds yet, but I guess they are affected as well)
Here’s a code which will crash the editor for sure.
using UnityEngine;
using System.Threading;
using System;
public class ThreadLauncher : MonoBehaviour {
Thread t;
// Use this for initialization
void Start () {
t = new Thread( new ThreadStart( ThreadRun ) );
t.Start();
}
public void ThreadRun()
{
for( int i = 0; i < 50; i++ )
{
Thread.Sleep(50); // wait a while
}
throw new Exception("Exception from a thread");
}
}
Just attach it to a camera and run the game.
Is there a way to catch unhandled exceptions from other threads? When will it be fixed in Unity? It would be nice to see a warning in editor when an exception occurs, not the runied IDE.
It seems I have filled a dozen of crash-reports today before I got to the reason for this problem!
Hello 
Have you sent that project as a bug report?
Exceptions are not being handled (as far as I know) at the moment in Unity.
Of course, I have. The latest report is 285446, where a clean WTR is provided. But thanks for advice, anyway )
People also say that a script should terminate its threads manually, that is Unity is not able to track them and stop when the game ends. Is it true?
Threads you spawn are your job to terminate again.
Thats base behavior when working with threads actually.
Question is what kind of stuff you want to use in own threads.
I’m asking because you have to ensure that your thread class does not use unity in any way. You can’t access unity (this means no monobehaviour access too) from within distinct threads without a guarantee that unity will crash
you must have a MonoBehaviour that access the thread data and uses it.
Hey,
Thanks for the nice small repro case in your bug report. This looks like a genuine bug. In the mean time, you could work around the issue by wrapping the thread’s meat in a big try/catch block.
Please note that while you can make and use threads in Unity, the Unity API itself is not threadsafe. That means you have to be very careful calling any unity functions from your second thread.
Bye, Lucas