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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:09:40+00:00 2026-05-12T08:09:40+00:00

Right now, I have a windows application (C#) that spits out long running reports.

  • 0

Right now, I have a windows application (C#) that spits out long running reports. Each report grabs some information from the UI to constrain the reports. Of course, the report creation happens on a background thread and I need to properly invoke the various controls to get things like selected indexes and values. But I don’t think my code looks good and was hoping there was a better pattern. Because there are so many requests to controls, I made generic delegates for each type that would be returned:

private delegate string StringDelegate();
private delegate int IntDelegate();

Further down, there are various instantiations of these delegates:

private StringDelegate GetYearSelectedItem = new StringDelegate(cmbYearAsync);

YearAsync looks like this:

private string cmbYearAsync() {
    return cmbYear.SelectedItem.ToString();
}

And finally, in code that is on a background thread, this is how I get the values:

cmbYear.Invoke(GetCmbYearSelectedItem);

Is there a cleaner way of getting these values from a separate thread?

  • 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-12T08:09:40+00:00Added an answer on May 12, 2026 at 8:09 am

    Assuming you want to go with the threaded design (perhaps you want the background threads to report partial results to the UI as they run?), if you have C# 3 you can certainly neaten it up a little. Some people find an extension method like this helpful:

    public static class ControlExtensions
    {
        public static T Invoke<T>(this Control ctrl, Func<T> func)
        {
            if (ctrl.InvokeRequired)
                return (T) ctrl.Invoke(func);
    
            return func();
        }
    }
    

    As well as checking whether the call needs to be marshalled, it also makes the delegate parameter typesafe. So you can write:

    string selected = comboBox1.Invoke(() => comboBox1.SelectedItem.ToString());
    

    No need to pre-declare all that other stuff. Just write the usual code you’d write but inside that lambda pattern.

    You can write a similar extension method that takes Action instead of Func<T>, and returns void, for operations that don’t need a return value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now i have some sort of services application on windows server 2003 for
I have multiple business entities in VB.NET Windows Forms application. Right now they are
Right now I have a Java application running WMI querying event logs, this is
I have a basic C# console application that executes a fairly long running process
I have a Java GUI-based application that writes some diagnostic messages to System.out and
I have an external Windows .exe that is actually Java application: Running the .exe
We have a script right now which our Windows users run on a Linux
I have a window that contains several rather complex views. Right now, I'm using
Right now I have this SQL query which is valid but always times out:
Right now I have something like this in NHibernate: Expression.Like(property, value, MatchMode.Anywhere) and that

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.