-1

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 関数を呼び出します。

それを行う必要がありますか、それともメソッドのユーザーが適切なスレッドで呼び出しを行うことを期待する必要がありますか?

4

1 に答える 1

1

そのメソッドがコントロール内にある場合は、常に UI スレッドで呼び出す必要があります。ユーザーが他のスレッドを呼び出したい場合、すべての WinForms コントロールと同様に、それを呼び出すのはユーザーの問題です。

于 2016-02-18T14:19:41.293 に答える