.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();
});
}
}