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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:38:47+00:00 2026-06-12T09:38:47+00:00

We currently have an application that reads data from serial communications. Throughout the application

  • 0

We currently have an application that reads data from serial communications. Throughout the application we need to display this data in the UI, and so we have created a method to update the textbox/label/numeric/progress bar using BeginInvoke.

However, this is starting to become cumbersome, as every control needs its own ‘Setter’, such that we already have 20 or so methods, which all do basically the same thing.

While I can see an easy way to genericize this to a specific control (e.g. only 1 for labels, 1 for textboxes), it would be ideal to only have 1 (maybe extension) method that we can call to update the data in the UI.

Here was our original method splayed everywhere:

private void SetCell1Cell2Async(decimal value)
    {
        if (spinCell1Cell2.InvokeRequired)
        {
            spinCell1Cell2.BeginInvoke(new EventHandler(delegate
            {
                spinCell1Cell2.Value = value;
            }));
        }
        else
        {
            if (spinCell1Cell2.IsDisposed) return; // Do not process if the control has been disposed of
            if (spinCell1Cell2.IsHandleCreated)
            {
                // This handle may not be created when creating this form AFTER data is already flowing
                // We could capture this data for future display (i.e. via deferUpdate = true or similar), but it is easier to ignore it
                // i.e. Do Nothing
                return;
            }
            spinCell1Cell2.Value = value;
        }
    }

Here is our current method (this works for Controls that use the Text property to display data):

delegate void SetTextAsyncCallback(Control ctrl, string text);
public static void SetTextAsync(this Control invoker, string text)
{
    if (invoker.InvokeRequired)
    {
        invoker.BeginInvoke(new SetTextAsyncCallback(SetTextAsync), invoker, text);
    }
    else
    {
        if (invoker.IsDisposed) return; // Do not process if the control has been disposed of
        if (!invoker.IsHandleCreated)
        {
            // This handle may not be created when creating this form AFTER data is already flowing
            // We could capture this data for future display (i.e. via deferUpdate = true or similar), but it is easier to ignore it
            // i.e. Do Nothing
            return;
        } 
        invoker.Text = text;
    }
}

As you may see, this method works with anything that uses the Text property to display its data.

Ideally I would like to be able to ‘pass in’ the property to update, and to supply a method that would take a string, double, decimal, boolean etc, but I’m a bit lost as where to go from here.

Any help appreciated.

  • 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-12T09:38:48+00:00Added an answer on June 12, 2026 at 9:38 am

    I would make a more universal solution, with something like this in the Form:

    delegate void ActionCallback();
    
    private void SetText()
    {
        this.SafeInvoke(() =>
            {
                if (spinCell1Cell2.IsDisposed) return;
                if (!spinCell1Cell2.IsHandleCreated) return;
                // Do stuff
            });
    }
    
    private void SafeInvoke(ActionCallback action)
    {
        if (this.InvokeRequired)
            this.Invoke(action);
        else
            action();
    }
    

    Using delegates, you only have to declare the actual action once, while the type of execution is decided later on (in the SafeInvoke-method). If you’d rather have some extension methods, here is an example:

    public delegate void ActionCallback();
    
    public static void SafeInvoke(this Form form, ActionCallback action)
    {
        if (form.InvokeRequired)
            form.Invoke(action);
        else
            action();
    }
    
    public static void SafeSetText(this Control ctl)
    {
        var form = ctl.FindForm();
        if (form == null) return; // or do something else
    
        form.SafeInvoke(() =>
            {
                // Do stuff
            });
    }
    

    Edit
    After reading your question again I noticed this wasn’t really what you asked about, so I’ll give you a more relevant example:

    public static void SafeUpdate<T>(this T ctl, ActionCallback action) where T : Control
    {
        var form = ctl.FindForm();
        if (form == null) return; // or do something else
    
        SafeInvoke(action);
    }
    
    // Then in some other method
    {
        textBox1.SafeUpdate(() =>
        {
            textBox1.Text = "123";
            textBox1.ForeColor = Color.Red;
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have an iPhone app that reads data from an external XML file
Currently i have an application that reads and writes several properties from one or
I have a simple application that reads data in small strings from large text
Scenario: I have an application that pulls data from a SQL database as well
I am writing a Google App Engine (Java) application that reads data from a
I have an application that reads from a COM Port and then does something
I am currently writing a linux application in C that reads from a configuration
I have a small POW -based application that stores data as HTML files. Currently
I have an application that reads a set of data files and performs some
I have a cakePHP application that is pulling data from two different databases, which

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.