0

getterでInvokeを使用したいのですが、4.0などではなく.Net2.0を使用する場合の方法を教えてください。.Net> 2.0の場合、使用できますFunc。.Net2.0の代わりとなるものは何ですか。

.Net 4.0の例を次に示します(リンクから)

public ApplicationViewModel SelectedApplication
{
    get {
            if (this.InvokeRequired) 
            {
                return (ApplicationViewModel)this.Invoke(new Func<ApplicationViewModel>(() => this.SelectedApplication));
            }
            else
            { 
                return _applicationsCombobox.SelectedItem as ApplicationViewModel;
            }
        }
}
4

2 に答える 2

2

.NET 2.0を使用しているため、デリゲートを使用することはできませんが、 MethodInvokerデリゲートFuncを使用することはできます。

.NET 2.0ではラムダ式の構文を使用できませんが、以下のコード例に示すように、「匿名デリゲート」構文(ほぼ同じもの)を使用できます。

UI以外のスレッドからUIコントロールのデータをクエリすることは、一般的には珍しいことです。通常、UIコントロールはUIスレッドで実行されるイベントをトリガーするため、その時点でUIコントロールから必要なデータを収集し、そのデータを他の関数に渡すので、呼び出しを行うことを心配する必要はありません。 。

ただし、あなたの場合は、次のようなことができるはずです。

public ApplicationViewModel SelectedApplication
{
    get
    {
        if (this.InvokeRequired)
        {
            ApplicationViewModel value = null; // compiler requires that we initialize this variable
            // the call to Invoke will block until the anonymous delegate has finished executing.
            this.Invoke((MethodInvoker)delegate
            {
                // anonymous delegate executing on UI thread due calling the Invoke method
                // assign the result to the value variable so that we can return it.
                value = _applicationsCombobox.SelectedItem as ApplicationViewModel;
            });
            return value;
        }
        else
        {
            return _applicationsCombobox.SelectedItem as ApplicationViewModel;
        }
    }
}

編集:.NET 4.0コードサンプルとInvoke関数を確認したので、値を返す方法を確認しました(以前に使用する理由がなかったものではありません)。

まあ、MethodInvokerデリゲートは戻り値を期待していませんが、@ haiyyuが指摘したように、独自のデリゲートを定義することができます。たとえば、独自のFunc<TResult>デリゲートを定義するだけで、元のコードはおそらく正常に機能します。

// this is all that is required to declare your own Func<TResult> delegate.
delegate TResult Func<TResult>();

MSDNページのサンプルコード:

public partial class Form1 : Form
{
    public Form1()
    {
        // Create a timer that will call the ShowTime method every second.
        var timer = new System.Threading.Timer(ShowTime, null, 0, 1000);           
    }

    private void ShowTime(object x)
    {
        // Don't do anything if the form's handle hasn't been created 
        // or the form has been disposed.
        if (!this.IsHandleCreated && !this.IsDisposed) return;

        // Invoke an anonymous method on the thread of the form.
        this.Invoke((MethodInvoker) delegate
        {
            // Show the current time in the form's title bar.
            this.Text = DateTime.Now.ToLongTimeString();
        });
    }
}
于 2012-02-22T15:01:21.193 に答える
1

デリゲートを使用します。デリゲートは一種の型付き関数ポインターです。ここにいくつかのより多くの読書があります:http://msdn.microsoft.com/en-us/library/ms173171%28v=vs.80%29.aspx

于 2012-02-22T14:39:44.853 に答える