Threading?

Ok i want to be able to create a thread.

in c# I do this

using UnityEngine;
using System.Collections;
using System.Threading;

public class threadScript : MonoBehaviour {

	void print()
	{
		Debug.Log("Hi from another thread!");
	}

	// Use this for initialization
	void Start () {
      // Create the thread object, passing in the Alpha.Beta method
      // via a ThreadStart delegate. This does not start the thread.
      Thread oThread = new Thread(new ThreadStart(print));

      // Start the thread
      oThread.Start();

      // Spin for a while waiting for the started thread to become
      // alive:
      while (!oThread.IsAlive);
      
      // Put the Main thread to sleep for 1 millisecond to allow oThread
      // to do some work:
      Thread.Sleep(1);
      
      // Request that oThread be stopped
      oThread.Abort();
      
      // Wait until oThread finishes. Join also has overloads
      // that take a millisecond interval or a TimeSpan object.
      oThread.Join();
      
      Debug.Log("print has finished");
      
      try 
      {
         Debug.Log("Try to restart the print thread");
         oThread.Start();
      }
      catch (ThreadStateException) 
      {
         Debug.Log("ThreadStateException trying to restart print. ");
         Debug.Log("Expected since aborted threads cannot be restarted.");
      }
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

or something like that when I do my thread (something more advanced that just shows that you can create a thread in c#.

I mainly use javascript though so is there anyway for example say make a function in c# to create a thread and stop one etc. which I can call in my javascript script (I’ve tried and failed)

how can I create a thread in javascript?

Edit: yes this is code from Microsoft’s website made to work in unity but it still works in unity and not my point.

you can not use any UnityEngine functionality in distinct Threads, this includes Debug etc

Yes partially you should be able to use threads from JS but I would likely avoid it as you don’t have documentations and the JS end is not having the full set of functionality so you might be missing exactly that functionality you wanted to use in your thread (where you have purely .NET non unity code)

false! I did run debug commands like log to console from another thread and unity engine commands from another thread :smile:. Do you mean in a standalone because I haven’t tried that.

But ok javascript threads aren’t implemented ok.

No I mean anywhere.
You jsut were lucky that not 2 threads called in at the same time to the same data.

Unity is not thread safe, accessing Unity from within a thread is a granted time bomb. Its not a matter of if it will just blow up, its a matter of how many seconds until it will.

oh ok. That makes sense.