Skip to main content
added 58 characters in body
Source Link
Nikita B
  • 13.1k
  • 1
  • 26
  • 57
  1. Your code is not thread-safe on many levels. Even if you replace regular increments/decrements with methods from Interlocked class, you will still need additional synchronization to make sure, that Callback is not called while Start is running.

  2. Term coroutine has a specific meaning in context of Unity, so you should probably use different word to avoid confusion.

  3. I don't know what is the recommended way of doing these things in Unity. In absence of TPL (I assume Unity still uses .Net 3.5), the easiest solution I can think of, that does not require good understanding of threading, is to return WaitHandles from your async methods:

    void Start()
    {
        var handles = new List<WiatHandle>();
        handles.Add(MyLogger.LogLastMonth());
        handles.Add(MyParser.ParseFiles());
        //etc...
        ThreadPool.QueueUserWorkItem(_ => 
        {
            WaitHandle.WaitAll(handles.ToArray());
    
            //at this point you might want to dispose handles 
            //if you do not plan to re-use them
    
            //Make sure this call is thread-safe.
            //If it is not, you will have to dispatch this call back to main thread
            //this callor backset tosome mainbool threadfield here, and move the call to Update.
            SceneManager.LoadScene("GameScene");
        });
    }
    
  1. Your code is not thread-safe on many levels. Even if you replace regular increments/decrements with methods from Interlocked class, you will still need additional synchronization to make sure, that Callback is not called while Start is running.

  2. Term coroutine has a specific meaning in context of Unity, so you should probably use different word to avoid confusion.

  3. I don't know what is the recommended way of doing these things in Unity. In absence of TPL (I assume Unity still uses .Net 3.5), the easiest solution I can think of, that does not require good understanding of threading, is to return WaitHandles from your async methods:

    void Start()
    {
        var handles = new List<WiatHandle>();
        handles.Add(MyLogger.LogLastMonth());
        handles.Add(MyParser.ParseFiles());
        //etc...
        ThreadPool.QueueUserWorkItem(_ => 
        {
            WaitHandle.WaitAll(handles.ToArray());
    
            //at this point you might want to dispose handles 
            //if you do not plan to re-use them
    
            //Make sure this call is thread-safe.
            //If it is not, you will have to dispatch 
            //this call back to main thread
            SceneManager.LoadScene("GameScene");
        });
    }
    
  1. Your code is not thread-safe on many levels. Even if you replace regular increments/decrements with methods from Interlocked class, you will still need additional synchronization to make sure, that Callback is not called while Start is running.

  2. Term coroutine has a specific meaning in context of Unity, so you should probably use different word to avoid confusion.

  3. I don't know what is the recommended way of doing these things in Unity. In absence of TPL (I assume Unity still uses .Net 3.5), the easiest solution I can think of, that does not require good understanding of threading, is to return WaitHandles from your async methods:

    void Start()
    {
        var handles = new List<WiatHandle>();
        handles.Add(MyLogger.LogLastMonth());
        handles.Add(MyParser.ParseFiles());
        //etc...
        ThreadPool.QueueUserWorkItem(_ => 
        {
            WaitHandle.WaitAll(handles.ToArray());
    
            //at this point you might want to dispose handles 
            //if you do not plan to re-use them
    
            //Make sure this call is thread-safe.
            //If it is not, you will have to dispatch this call back to main thread
            // or set some bool field here, and move the call to Update.
            SceneManager.LoadScene("GameScene");
        });
    }
    
added 164 characters in body
Source Link
Nikita B
  • 13.1k
  • 1
  • 26
  • 57
  1. Your code is not thread-safe on many levels. Even if you replace regular increments/decrements with methods from Interlocked class, you will still need additional synchronization to make sure, that Callback is not called while Start is running.

  2. Term coroutine has a specific meaning in context of Unity, so you should probably use different word to avoid confusion.

  3. I don't know what is the recommended way of doing these things in Unity. In absence of TPL (I assume Unity still uses .Net 3.5), the easiest solution I can think of, that does not require good understanding of threading, is to return WaitHandles from your async methods:

    void Start()
    {
        var handles = new List<WiatHandle>();
        handles.Add(MyLogger.LogLastMonth());
        handles.Add(MyParser.ParseFiles());
        //etc...
        ThreadPool.QueueUserWorkItem(_ => 
        {
            WaitHandle.WaitAll(handles.ToArray());
    
            //at this point you might want to dispose handles 
            //if you do not plan to re-use them
    
            //Make sure this call is thread-safe.
            //If it is not, you will have to dispatch 
            //this call back to main thread
            SceneManager.LoadScene("GameScene");
        });
    }
    
  1. Your code is not thread-safe on many levels. Even if you replace regular increments/decrements with methods from Interlocked class, you will still need additional synchronization to make sure, that Callback is not called while Start is running.

  2. Term coroutine has a specific meaning in context of Unity, so you should probably use different word to avoid confusion.

  3. I don't know what is the recommended way of doing these things in Unity. In absence of TPL (I assume Unity still uses .Net 3.5), the easiest solution I can think of, that does not require good understanding of threading, is to return WaitHandles from your async methods:

    void Start()
    {
        var handles = new List<WiatHandle>();
        handles.Add(MyLogger.LogLastMonth());
        handles.Add(MyParser.ParseFiles());
        //etc...
        ThreadPool.QueueUserWorkItem(_ => 
        {
            WaitHandle.WaitAll(handles.ToArray());
    
            //at this point you might want to dispose handles 
            //if you do not plan to re-use them
    
            SceneManager.LoadScene("GameScene");
        });
    }
    
  1. Your code is not thread-safe on many levels. Even if you replace regular increments/decrements with methods from Interlocked class, you will still need additional synchronization to make sure, that Callback is not called while Start is running.

  2. Term coroutine has a specific meaning in context of Unity, so you should probably use different word to avoid confusion.

  3. I don't know what is the recommended way of doing these things in Unity. In absence of TPL (I assume Unity still uses .Net 3.5), the easiest solution I can think of, that does not require good understanding of threading, is to return WaitHandles from your async methods:

    void Start()
    {
        var handles = new List<WiatHandle>();
        handles.Add(MyLogger.LogLastMonth());
        handles.Add(MyParser.ParseFiles());
        //etc...
        ThreadPool.QueueUserWorkItem(_ => 
        {
            WaitHandle.WaitAll(handles.ToArray());
    
            //at this point you might want to dispose handles 
            //if you do not plan to re-use them
    
            //Make sure this call is thread-safe.
            //If it is not, you will have to dispatch 
            //this call back to main thread
            SceneManager.LoadScene("GameScene");
        });
    }
    
added 111 characters in body
Source Link
Nikita B
  • 13.1k
  • 1
  • 26
  • 57
  1. Your code is not thread-safe on many levels. Even if you replace regular increments/decrements with methods from Interlocked class, you will still need additional synchronization to make sure, that Callback is not called while Start is running.

  2. Term coroutine has a specific meaning in context of Unity, so you should probably use different word to avoid confusion.

  3. I don't know what is the recommended way of doing these things in Unity. In absence of TPL (I assume Unity still uses .Net 3.5), the easiest solution I can think of, that does not require good understanding of threading, is to return wait handlesWaitHandles from your async methods:

    void Start()
    {
        var handles = new List<WiatHandle>();
        handles.Add(MyLogger.LogLastMonth());
        handles.Add(MyParser.ParseFiles());
        //etc...
        ThreadPool.QueueUserWorkItem(_ => 
        {
            WaitHandle.WaitAll(handles.ToArray());
    
            //at this point you might want to dispose handles 
            //if you do not plan to re-use them
    
            SceneManager.LoadScene("GameScene");
        });
    }
    
  1. Your code is not thread-safe on many levels. Even if you replace regular increments/decrements with methods from Interlocked class, you will still need additional synchronization to make sure, that Callback is not called while Start is running.

  2. Term coroutine has a specific meaning in context of Unity, so you should probably use different word to avoid confusion.

  3. I don't know what is the recommended way of doing these things in Unity. In absence of TPL (I assume Unity still uses .Net 3.5), the easiest solution, that does not require good understanding of threading, is to return wait handles from your async methods:

    void Start()
    {
        var handles = new List<WiatHandle>();
        handles.Add(MyLogger.LogLastMonth());
        handles.Add(MyParser.ParseFiles());
        //etc...
        ThreadPool.QueueUserWorkItem(_ => 
        {
            WaitHandle.WaitAll(handles.ToArray());
    
            //at this point you might want to dispose handles 
            //if you do not plan to re-use them
    
            SceneManager.LoadScene("GameScene");
        });
    }
    
  1. Your code is not thread-safe on many levels. Even if you replace regular increments/decrements with methods from Interlocked class, you will still need additional synchronization to make sure, that Callback is not called while Start is running.

  2. Term coroutine has a specific meaning in context of Unity, so you should probably use different word to avoid confusion.

  3. I don't know what is the recommended way of doing these things in Unity. In absence of TPL (I assume Unity still uses .Net 3.5), the easiest solution I can think of, that does not require good understanding of threading, is to return WaitHandles from your async methods:

    void Start()
    {
        var handles = new List<WiatHandle>();
        handles.Add(MyLogger.LogLastMonth());
        handles.Add(MyParser.ParseFiles());
        //etc...
        ThreadPool.QueueUserWorkItem(_ => 
        {
            WaitHandle.WaitAll(handles.ToArray());
    
            //at this point you might want to dispose handles 
            //if you do not plan to re-use them
    
            SceneManager.LoadScene("GameScene");
        });
    }
    
added 2 characters in body
Source Link
Nikita B
  • 13.1k
  • 1
  • 26
  • 57
Loading
Source Link
Nikita B
  • 13.1k
  • 1
  • 26
  • 57
Loading