0

I would like to use Invoke in getter, how to do it when using .Net 2.0 not e.g. 4.0? For .Net > 2.0 we can use Func and what is replacement for .Net 2.0?

Here is example for .Net 4.0 (from link)

public ApplicationViewModel SelectedApplication
{
    get {
            if (this.InvokeRequired) 
            {
                return (ApplicationViewModel)this.Invoke(new Func<ApplicationViewModel>(() => this.SelectedApplication));
            }
            else
            { 
                return _applicationsCombobox.SelectedItem as ApplicationViewModel;
            }
        }
}
Community
  • 1
  • 1
jotbek
  • 1,479
  • 3
  • 14
  • 22

2 Answers2

2

Since you're using .NET 2.0, you won't have the Func delegate available to you, but you can use the MethodInvoker delegate.

You won't be able to use the lambda expression syntax with .NET 2.0, but you can use the "anonymous delegate" syntax (which is pretty much the same thing), as shown in the code example below.

Querying data in UI controls from a non-UI thread is generally an uncommon thing to do; usually your UI controls trigger events that execute on the UI thread, so you gather the data you need from your UI controls at that time and then pass that data on to some other function, so you don't need to worry about doing an Invoke.

In your case, though, you should be able to do something like this:

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;
        }
    }
}

EDIT: Now that I look at your .NET 4.0 code sample and also look at the Invoke function, I see how it can return a value (not something that I've had a reason to use before).

Well, the MethodInvoker delegate does not expect a return value, but as @haiyyu pointed out, you could define your own delegate. For instance, you would just need to define your own Func<TResult> delegate, and the original code would probably work fine:

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

Sample code from the MSDN page:

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();
        });
    }
}
Dr. Wily's Apprentice
  • 10,212
  • 1
  • 25
  • 27
1

Use delegates, they are a sort of typed function pointers. Here's some more reading: http://msdn.microsoft.com/en-us/library/ms173171%28v=vs.80%29.aspx

Svarog
  • 2,188
  • 15
  • 21