1

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!

Community
  • 1
  • 1
Sinker
  • 576
  • 1
  • 7
  • 21
  • I don't know the syntax of JScript.Net, but have you tried `var workDelegate : ThreadStart = workerClass.PerformCalculation;`? – svick Jul 31 '12 at 07:24
  • Compiles fine @svick . I will try an run it. Thanks. – Sinker Jul 31 '12 at 07:32
  • It definitely worked as needed @svick . Do you want to copy what you wrote above as an answer so I can designate it as the correct answer. – Sinker Jul 31 '12 at 08:29

1 Answers1

1

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;
svick
  • 236,525
  • 50
  • 385
  • 514