【问题标题】:How to run a method in a new thread in Jscript .NET如何在 Jscript .NET 的新线程中运行方法
【发布时间】:2012-07-31 07:20:03
【问题描述】:

我一直在寻找答案 2 天了,我真的被困住了!我找到了这个question,但答案不起作用。

我有一个 jscript 类 WorkerClass,它有一个函数 PerformCalculation,它需要在单独的线程上运行,因为它不能在 UI 线程上运行。

这是我的解决方案:

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();
}


我尝试了一些东西,例如:

  • PerformCalculation 函数放入CallerClass
  • PerformCalculation 函数放在一个单独的类中,即WorkerClass
  • 创建PerformCalculation 函数privatepublicstatic 并且没有访问修饰符(默认)
  • var workerThread : Thread = new Thread(workerClass.PerformCalculation);

在前三个场景中,我得到了以下编译时错误:

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

最后一个场景给出了以下编译时错误:

More than one constructor matches this argument list


您认为我的代码有什么问题,我该如何解决?

提前致谢!

【问题讨论】:

  • 我不知道JScript.Net的语法,但是你试过var workDelegate : ThreadStart = workerClass.PerformCalculation;吗?
  • 编译良好 @svick 。我会尝试运行它。谢谢。
  • 它确实可以按需工作@svick。你想复制你上面写的作为答案,以便我可以将其指定为正确答案。

标签: .net multithreading jscript jscript.net


【解决方案1】:

我认为错误信息清楚地说明了需要做什么:构造委托,不要使用new

var workDelegate : ThreadStart = workerClass.PerformCalculation;

【讨论】:

    猜你喜欢
    • 2014-01-09
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多