How to run a method in a new thread in Jscript .NET

671 Views Asked by At

I have been looking for an answer for 2 days now and I am truly stuck! I found this question, but the answer did not work.

I have a jscript class, WorkerClass which has a function, PerformCalculation, that needs to run on a separate thread because it cannot run on the UI thread.

This is my solution:

CallerClass

private function PerformCalculation() {
    var workerClass = new WorkerClass(parameter1, parameter2, parameter3);
    var workDelegate : ThreadStart = new ThreadStart(workerClass.PerformCalculation);
    var workerThread : Thread = new Thread(workDelegate);
    workerThread.Start();
    workerThread.Join();
}


I have tried a few things, such as:

  • Putting the PerformCalculation function in the CallerClass
  • Putting the PerformCalculation function in a separate class, i.e. WorkerClass
  • Making the PerformCalculation function private, public, static and with no access modifier (default)
  • var workerThread : Thread = new Thread(workerClass.PerformCalculation);

In the first three scenarios, I got the following compile-time error:

Delegates should not be explicitly constructed, simply use the method name

and the last scenario gives the following compile-time error:

More than one constructor matches this argument list


What do you think is the problem with my code and how can I fix it?

Thanks in advance!

1

There are 1 best solutions below

0
On BEST ANSWER

I think the error message says clearly what needs to be done: to construct a delegate, don't use new:

var workDelegate : ThreadStart = workerClass.PerformCalculation;