- Home /
Question by
andrewnobody · Jun 24, 2013 at 09:39 PM ·
javascriptvariablesthreadingpassingarguments
Passing a variable to thread function in Javascript
How do I pass variables to thread?
#pragma strict
function Foo(MyVar : int) {
for(var i=0;i<10000;i++) {
MyVar = i;
}
}
function Start () {
var VarIwantToPass : int = 123;
var thread = System.Threading.Thread(Foo ( VarIwantToPass ) );
thread.Start();
}
// Error is :
// BCE0024: The type 'System.Threading.Thread' does not have a
// visible constructor that matches the argument list '(void)'.
I saw some crazy solutions but for C# eg. like this:
Thread t = new Thread( () => send2( var1, var2 ) );
How can I use something similar in JavaScript?
Comment
Best Answer
Answer by Eric5h5 · Jun 24, 2013 at 09:44 PM
Pass the parameter using Thread.Start. e.g.,
import System.Threading;
function Start () {
var thread = new Thread (Foo);
thread.Start (123);
}
What about more variables? Like 2, 3 or more? Is there any way to do this?
And thank you! This code is working, but only for 1 var. It's enough for now but I may need to pass more variables later.
What I do is make a class that contains the variable types I need, and pass in a variable of that class. For example, if I needed two ints, then I'd have
class Foo {
var int1 : int;
var int2 : int;
}