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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:38:27+00:00 2026-06-04T14:38:27+00:00

I have a windows form with a text box txtOutput . I have some

  • 0

I have a windows form with a text box txtOutput. I have some content in it. I have written a property to get and set the txtOutput.Text both from within the same thread and across threads like this:

public string OutputString
{
    get
    {
        string text = string.Empty;
        if (txtOutput.InvokeRequired)
        {
            txtOutput.BeginInvoke(new MethodInvoker(delegate
                {
                    text = txtOutput.Text;
                }));
        }
        else
        {
            text = txtOutput.Text;
        }
        return text;
    }
    set
    {
        if (txtOutput.InvokeRequired)
        {
            txtOutput.BeginInvoke(new MethodInvoker(delegate
                {
                    txtOutput.Text = value;
                }));
        }
        else
        {
            txtOutput.Text = value;
        }
    }
}

If I set/get the property from the same thread, the behavior is as expected when calling the below function like PrintMessage().

public void PrintMessage()
{
    MessageBox.Show(OutputString);
}

But when I call like this new Thread(PrintMessage).Start(). The get does not retrieve the value in the text box (i.e., the MessageBox shows empty string). When I do the same by keeping a breakpoint on the line:

txtOutput.BeginInvoke(new MethodInvoker(delegate
{
    text = txtOutput.Text;
}));

while debug, the value is retrieved (i.e., the MessageBox shows the txtOutput content)

Should I sleep somewhere? Where am I making the mistake?

  • 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-04T14:38:28+00:00Added an answer on June 4, 2026 at 2:38 pm

    The problem is you’re calling MessageBox.Show() with a reference to the text variable before the UI thread can handle the request you placed with the dispatcher. I would avoid using Thread.Sleep() as you could end up with some nasty side effects. Ideally you should re-factor your code to get rid of the property, which is synchronous, in favor of a more asynchronous solution. Something similar to the code below should give you the result you’re looking for:

    public void PrintMessage(Action<string> displayAction)
    {
        string text = string.Empty;
        if (txtOutput.InvokeRequired)
        {
            txtOutput.BeginInvoke(new MethodInvoker(delegate
            {
                displayAction.Invoke(txtOutput.Text);
            }));
        }
        else
        {
            displayAction.Invoke(txtOutput.Text);
        }
    }
    

    And Invoke it:

    // Get the text asynchronously
    PrintMessage(s => MessageBox.Show(s));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

net windows form application. I have a combo box and a text box and
In my windows form i have two combobox and one text box when a
In my windows form I have a text box and a button, the text
I have a windows form application which has a text box and 3 buttons.
I have windows application in c# with a form with 7 text box that
I have a windows form 'MyForm' with a text box that is bound to
I have a windows form with a text box: txtMyText. txtMyText.Text is Bound to
I have a Windows Form with a RichTextBox on it. The content of the
I have a form with a single text box on it. No other controls.
I have a textbox and some buttons on a form in my windows application.

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.