代码之家  ›  专栏  ›  技术社区  ›  Callum Rogers

尝试调用委托时出现“方法不受支持”错误

  •  4
  • Callum Rogers  · 技术社区  · 15 年前

    我有一个功能 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);
    }
    

    但是,我得到一个 NotSupportedException was unhandled by user code 带着…的信息 Specified method is not supported 对于 BeginInvoke() 委托的方法。怎么了?

    我正在使用Silverlight 4.0和VS2010。

    1 回复  |  直到 15 年前
        1
  •  7
  •   iCollect.it Ltd    15 年前

    Asynchronous Delegate.BeginInvoke不可用于Silverlight中的委托。

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