JS ライブラリをラップし、Web ブラウザ コントロールを拡張する Winforms コントロールを作成しています。
次のように JavaScript 関数を呼び出しています。
/// <summary>
/// Asks the browser to run a JavaScript function
/// </summary>
/// <param name="name">The name of the JS function. WARNING usually browser JS engines make the first letter of the function lowercase. If you pass 'Foo', the browser will look for 'foo'</param>
/// <param name="args">The arguments to pass to the method; Should probably be JSON / JSON string.</param>
/// <returns>The object that the JS function returned</returns>
private object MyInvokeScript(string name, params object[] args)
{
//If we're not on the main thread (the one that owns the control), invoke yourself, in the correct thread.
if (InvokeRequired)
return this.Invoke(new ScriptInvokationHandler(MyInvokeScript), new Object[] { name, args });
else //else, just call the script
return this.Document.InvokeScript(name, args);
}
このメソッドは、公開されているすべてのメソッドを呼び出すために使用され、name
パラメーターに基づいて JS 関数を呼び出します。
それを行う必要がありますか、それともメソッドのユーザーが適切なスレッドで呼び出しを行うことを期待する必要がありますか?