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 8398399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:02:47+00:00 2026-06-09T21:02:47+00:00

After following this question on updating a GUI from another thread I wanted to

  • 0

After following this question on updating a GUI from another thread I wanted to extend the code slightly so that it worked for something other than property assignment. Specifically I was trying to find a way to assign some functionality directly to a lambda so that I can define the behavior as needed (I modified the original slightly for WPF):

    private delegate void UpdateControlThreadSafeDelegate(Control control, System.Linq.Expressions.Expression<Action> property);
    public void UpdateControl(Control control, System.Linq.Expressions.Expression<Action> property)
    {

        // If calling thread is not associated with control dispatcher, call our thread safe property update delegate
        if (!control.Dispatcher.CheckAccess())
        {
            control.Dispatcher.Invoke(new UpdateControlThreadSafeDelegate(UpdateControl), new object[] { control, property });
        }
        else
        {
            Action call = property.Compile();
            call();
        }
    }

With usage:

UpdateControl(lbFoo, () => lbFoo.Items.Clear()); // where lbFoo is a ListBox control

This works fine. But I’d rather allow do something like:

UpdateControl(lbFoo, () => { lbFoo.Items.Clear(); lbFoo.Items.Add("Bar");});

This does not work, returning error CS0834: A lambda expression with a statement body cannot be converted to an expression tree. The error is clear, I’m just not certain on how best to proceed. I could follow my original usage and do what I need in several lines, it’s just not as tidy.

I’m guessing there is a better/easier way to do what I want.

  • 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-06-09T21:02:49+00:00Added an answer on June 9, 2026 at 9:02 pm

    If you don’t use expressions, and just pass the action, like so:

    public void UpdateControl(Control control, Action actionToExecute)
    

    Then you can use this as written. The only other change will be your else statement, where you would just call this directly:

       else
        {
            actionToExecute();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After answering this question I put together the following C# code just for fun:
Following up from this question: How can I unlock a file that is locked
Similar to this question , after running the following code the browser dialog does
FINAL EDIT: After following the answer from Darin Dimitrov, I have found that the
After following this question , I added HttpServletRequest parameter to my method. But I
This question is based on the thread . I have the following Git-prompt at
After asking this question : jQuery die() does not work . I found that
This question came to mind after seeing this simple piece of code: if (!x%y)
Following on from this question about setting Culture, I have a similar issue with
After following the advice in this question successfully, I added a couple additional lines

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.