Search Unity

Unity Editor Threads

Discussion in 'Editor & General Support' started by CDF, Jul 22, 2014.

  1. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,312
    Hi, struggling a little with threads inside the unity editor.

    It seems my threads don't stay active after unity re-compiles.
    Is there anyway I can keep a thread "working" during a recompile?
    I'm using an EditorWindow, here's a really simple script:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5. using System.Threading;
    6.  
    7. public class TestWindow : EditorWindow {
    8.  
    9.     private static Worker workerObject;
    10.     private static Thread workerThread;
    11.  
    12.     void OnGUI() {
    13.  
    14.         if (GUILayout.Button("Start")) {
    15.  
    16.             workerObject = new Worker();
    17.             workerThread = new Thread(workerObject.DoWork);
    18.             workerThread.Start();
    19.         }
    20.  
    21.         if (GUILayout.Button("Stop")) {
    22.  
    23.             workerObject.RequestStop();
    24.         }
    25.     }
    26.  
    27.     public class Worker {
    28.      
    29.         public void DoWork() {
    30.          
    31.             while (!_shouldStop) {
    32.              
    33.                 Debug.Log("worker thread: working...");
    34.  
    35.                 Thread.Sleep(1000);
    36.             }
    37.          
    38.             Debug.Log("worker thread: terminating gracefully.");
    39.         }
    40.      
    41.         public void RequestStop() {
    42.          
    43.             _shouldStop = true;
    44.         }
    45.  
    46.         private volatile bool _shouldStop;
    47.     }
    48. }
    49.  
    Even though it's not a background thread, it gets stopped whenever unity compiles :(
     
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,312
    guess not then.
     
  3. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Yes, unfortunately that is not possible as such. When Unity recompiles, it recreates the entire AppDomain that the game and the editor is running in. This essentially means that the entire managed application state is killed off and then recreated. The reason for this is that it's not possible to unload/reload DLLs that have already been loaded into AppDomains.

    The only thing that could survive is running stuff in another AppDomain but... you wouldn't want to go there :)

    What you can do, however, is have a private field that indicates whether the worker is running and then in Awake() spawn the worker thread if the field is set. This will work as Unity includes private fields in the backups that are being made before an AppDomain is torn down. Thus the value will survive in this case whereas if the MonoBehaviour gets saved to and loaded from disk, the field will come out with its default value.
     
  4. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,312
    yeah, I guess my main issue is that all data within the thread is lost.

    I'm also finding that whenever I hit play, data is erased as well in the EditorWindow :(
     
  5. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Yeah, surviving a reload can be tricky and usually requires a bit of extra work. However, it is usually quite doable as the editor itself proves.

    Make sure that the state you want to bring over can and will be serialized at least as part of the private state you have. Then, when you come back online, update running state from persisted information.
     
  6. mr malee

    mr malee

    Joined:
    Sep 19, 2012
    Posts:
    25
    I'm trying to write an editor socket server, so I think my only option is to externalize the server into a .exe and .app file which the editor can open