Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to stop Threading?

Discussion in 'Getting Started' started by phong75, Jul 1, 2023.

  1. phong75

    phong75

    Joined:
    Jan 2, 2023
    Posts:
    2
    I have a following code:

    Code (CSharp):
    1. Thread thread;
    2.  
    3.     void Start()
    4.     {
    5.         thread = new Thread (() => Test ());
    6.         thread.Start ();
    7.     }
    8.     public void Test()
    9.     {
    10.         int test_ID = 0;
    11.         for (long i = 0; i < 1000000000; i++) {
    12.             test_ID++;
    13.         }
    14.         Debug.Log (test_ID);
    15.     }
    When I pressed Play and quickly pressed Play again (that means I was out of the Play window), this code continued to run. How to stop it?
     
    Last edited: Jul 2, 2023
  2. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    Press stop! Simples!

    Also, when you're displaying code, please use CODE tags.
     
  3. phong75

    phong75

    Joined:
    Jan 2, 2023
    Posts:
    2
    Thank but I don't understand. What do you mean?
     
  4. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    English isn't your first language?

    There's a simple tag in the post, when you're displaying code, click the CODE button, and then write out your code.
     
    phong75 likes this.
  5. meredoth

    meredoth

    Joined:
    Jan 29, 2014
    Posts:
    7
    Threads don't stop automatically when you exit play mode in Unity, you have to do it manually.
    There is no stop button for stopping threads after exiting play mode.
    Also the Unity API doesn't work in threads because it is not thread safe.

    That being said I don't recommend using threads in Unity for asynchronous code, try Tasks that can have the same problems, even better Unitask or if you are in the 2023 version the new awaitable https://docs.unity3d.com/2023.1/Documentation/ScriptReference/Awaitable.html
    you can also look at the job system:
    https://docs.unity3d.com/Manual/JobSystem.html

    In any case, for your example something like this works:
    Code (CSharp):
    1.  
    2. Thread thread;
    3.     CancellationTokenSource cts = new();
    4.  
    5.     void Start()
    6.     {
    7.         thread = new Thread (() => Test(cts.Token));
    8.         thread.Start ();
    9.     }
    10.  
    11.     public void Test(CancellationToken token)
    12.     {
    13.         int test_ID = 0;
    14.         for (long i = 0; i < 1_000_000_000; i++)
    15.         {
    16.             if (token.IsCancellationRequested)
    17.                 break;
    18.          
    19.             test_ID++;
    20.         }
    21.         Debug.Log(test_ID);
    22.     }
    23.  
    24.     private void OnDestroy()
    25.     {
    26.         cts?.Cancel();
    27.         cts?.Dispose();
    28.     }
    this stops the thread manually in the OnDestroy method
     
    phong75 likes this.