【问题标题】:"Method is not supported" error when trying to invoke a delegate尝试调用委托时出现“不支持方法”错误
【发布时间】:2010-08-18 19:58:08
【问题描述】:

我有一个函数Run(string, string[]),我想在单独的线程上运行它,所以我使用了一个委托和BeginInvoke

private Func<string, string[], Stack<StackItem>> runner;

public MainPage()
{
    runner = Run;
}

private void btnStep_Click(object sender, RoutedEventArgs e)
{
    // snip
    runner.BeginInvoke(tbCode.Text, GetArgs(), null, null); // Exception here
    // snip
}

private Stack<StackItem> Run(string program, string[] args)
{
    return interpreter.InterpretArgs(parser.Parse(lexer.Analyse(program)), args);
}

但是,对于委托的 BeginInvoke() 方法,我收到了带有 Specified method is not supported 消息的 NotSupportedException was unhandled by user code。怎么了?

我正在使用 Silverlight 4.0 和 VS2010。

【问题讨论】:

  • 委托有三个参数,你只传递了两个。这是如何编译的?
  • @Hans:委托有 2 个参数,Stack&lt;StackItem&gt; 是返回类型(它是 Func 而不是 Action)。
  • 啊!咳嗽,是的。谢谢!

标签: c# .net multithreading silverlight delegates


【解决方案1】:

异步 ​​Delegate.BeginInvoke 不适用于 Silverlight 中的委托。

您应该使用BackgroundWorker instead 来异步运行任何东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2012-08-17
    • 1970-01-01
    • 2021-03-12
    相关资源
    最近更新 更多