Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7532633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:30:40+00:00 2026-05-30T05:30:40+00:00

I would like to use Invoke in getter, how to do it when using

  • 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;
            }
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:30:42+00:00Added an answer on May 30, 2026 at 5:30 am

    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();
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using AdoNetAppender (SQL server) in my asp.net application and would like use
I would like to get certificate from Store using CryptoAPI P/Invoke. But I encountered
Is it possible to use OCaml with .NET code? I would like to invoke
I would like to use something like CLR Profiles on .Net 2.0 to see
I would like to use VB9 but am not sure what syntax to use
I would like to use a language that I am familiar with - Java,
I would like to use as and is as members of an enumeration. I
I would like to use a component that exposes the datasource property, but instead
I would like to use client-side Javascript to perform a DNS lookup (hostname to
I would like to use javascript to develop general-purpose GUI applications. Initially these are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.