2010-08-18 72 views
4

我有一個函數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消息。出了什麼問題?

我正在使用Silverlight 4.0和VS2010。

+0

委託人有三個參數,你只傳遞兩個參數。這是如何編譯的? – 2010-08-18 20:41:10

+1

@Hans:代表有2個參數,'Stack '是返回類型(它是'Func'而不是'Action')。 – 2010-08-18 21:44:51

+0

啊!咳嗽,是的。謝謝! – 2010-08-18 21:49:42

回答

7

異步Delegate.BeginInvoke不適用於Silverlight中的委託。

您應該使用BackgroundWorker instead異步運行任何東西。

+3

真的嗎?我想知道爲什麼'BeginInvoke()'不被允許? – 2010-08-18 20:30:26