Repeating Threads?

Hello!I have a little issue with a thread function that has to repeat as soon as it is over…but it seems the way i am doing it throws the “Too many threads” error…

This is the way i am doing it :

import System.Threading;

var mThread : Thread;
var repeat : boolean = true;

function Update(){ //Or any other function...
if(repeat){
var ts : ThreadStart = new ThreadStart(myFunction);
mThread = new Thread(ts);
mThread.Start();
repeat = false;
}
}

function myFunction(){

mThread.Sleep(0);

//Derp

repeat = true;
mThread.Abort();

}

Please help me!
Baisically that function is called only once it is done,and not overlapping with another instance of itself…Technically…
Thank you!

Ah,easy fix!

import System.Threading;

var mThread : Thread;
  
function Awake() {
    mThread = new Thread(ThreadWorker);    
  }

function Start(){
mThread.Start();
}

  
function ThreadWorker(){

while(true){
Thread.Sleep(0);

print("Started!");
Thread.Sleep(5000);


print("Ended!");
}

}