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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[C#]Use Thread in unity

Discussion in 'Scripting' started by _HAL_9000, Apr 8, 2016.

  1. _HAL_9000

    _HAL_9000

    Joined:
    Jun 26, 2015
    Posts:
    48
    Hello everyones !
    I am trying to use thread in unity to write in files, I am not using unity API inside of my threaded functions.
    It appears thread are not working for me ...
    All I get is many error messages in visual studio.

    I tried copying msdn code (like from here) but I always get error messages. Then I looked on the asset store and test this but I got many compilation error from unity. I tried the code from many other msdn pages and unity forum solved post but it didn't help.

    Among all the errors I get I always get error CS1729 when creating thread like :
    Code (CSharp):
    1.         Thread _thread= new Thread(AnObject.Afunction);
    Where "anObject" is a class containing a method "Afunction".
    I don't know what to do it seems many people are using thread easily and I would like to be able to do so.

    Please help me unity developpers you are my only hope ... :(
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Are you using the namespace system.threading?
     
  3. _HAL_9000

    _HAL_9000

    Joined:
    Jun 26, 2015
    Posts:
    48
    Hello and thanks for your response !
    Yes I am.
     
  4. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    paste your entire script and we may better be able to assist you
     
  5. _HAL_9000

    _HAL_9000

    Joined:
    Jun 26, 2015
    Posts:
    48
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Threading;
    4.  
    5. public class Thread : MonoBehaviour {
    6.  
    7.     [System.Serializable]
    8.     public class threadHandle
    9.     {
    10.         public  void    calculus()
    11.         {
    12.             // do something
    13.         }
    14.     }
    15.  
    16.     void Start () {
    17.         threadHandle _handle = new threadHandle();
    18.         Thread thread = new Thread(_handle.calculus);
    19.  
    20.         thread.Start();
    21.     }
    22.  
    23.     void Update () {
    24.    
    25.     }
    26. }
    27.  
     
  6. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Your class is named Thread. It uses that instead of System.Threading.Thread. I suggest to rename it, however you can replace Thread inside it with direct disambiguation by using System.Threading.Thread instead.
     
  7. _HAL_9000

    _HAL_9000

    Joined:
    Jun 26, 2015
    Posts:
    48
    I tried to use "new ThreadStart(_handle.calculus)" on the "new Thread()" but it doesn't work either.
     
  8. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Just rename your class from "public class Thread" to "public class ThreadWorker" and rename file to same as well because otherwise Unity won't pick up monobehaviour.
     
    _HAL_9000 likes this.
  9. _HAL_9000

    _HAL_9000

    Joined:
    Jun 26, 2015
    Posts:
    48
    Indeed folks ... Thank you !
     
  10. DanHedges

    DanHedges

    Joined:
    Jan 21, 2016
    Posts:
    77
    Sorry, that reply went a little wrong!

    What I was trying to do before my browser reloaded was change the code to have ThreadWorker as the name of the class (as previously suggested) AND the thread start (which you will need) in the declaration of the thread!

    I can try again unless you have already changed your code!

    Dan
     
  11. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Yeah, don't overrite .NET names with your classes lol