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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:11:28+00:00 2026-05-16T01:11:28+00:00

I’m using the following method to show a modeless Message Box. public void ShowMessageBox(string

  • 0

I’m using the following method to show a modeless Message Box.

public void ShowMessageBox(string Message)
{
  var thread = new Thread(
    () =>
    {
      MessageBox.Show(Message);
    });
  thread.Start();
}

The “() => {…}” is something I’ve never seen before. What is the name for this code pattern?

Also, thread.Start starts the thread, and it automatically closes once the “()=>{…}” method completes (when the Message Box is OK’ed), right? If so, can you please point me to some official documentation saying that the thread closes automatically?

Thanks!

  • 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-16T01:11:29+00:00Added an answer on May 16, 2026 at 1:11 am

    It’s the lambda operator, and read as “goes to”. MSDN has a good intro: Lambda Expressions (C# Programming Guide)

    One concern with your example is that you’re spinning up a new thread to update the UI, the UI is intrinsically single-threaded, so background updates are generally the wrong thing to do (unless you’re manually/explicitly checking InvokeRequired and calling Invoke() as needed.


    Regarding the UI threading…

    In WinForms every Form or Control is created on a particular thread (the “UI Thread”), and you can think of that thread as owning that control (not exactly correct, but a good way to conceptualize it). Updating the UI from that thread is safe, updating the UI from another thread runs the risk of collisions and corruption and all the usual risks of parallel/async programming.

    …So… how do you safely update the UI from a background thread without blocking the UI? In short–you can’t–the best you can do is block it for the bare minimum required to update the UI. This is where InvokeRequired and Invoke() come in…

    Here’s a sample: you should be able to drop this into the code-behind of a new form with a button and textbox.

    To use:

    • Try commenting out either the call to SetTextAsyncSafe() or SetTextAsyncSafe() — running both could confuse you since they won’t necessarily execute in the order they’re called (they’re running async, remember?).

    • Then set a breakpoint on SetText(). You should see the “safe” call will actually call the method twice–the first call will detect InvokeRequired and will call the method a 2nd time for the correct thread by Invoke()‘ing to it.

    • You should see an Exception thrown when SetTextAsyncUnsafe() actually gets to the textBox1.Text = value; statements. The exception will be an InvalidOperationException with a message stating “Cross-thread operation not valid” — you can google this term for more details.

    The code:

    private void button1_Click(object sender, EventArgs e)
    {
      SetTextAsyncSafe("This update was made from the UI Thread by using Invoke()");
      SetTextAsyncUnsafe("This update was made directly from the background thread and can cause problems");
    }
    
    private void SetTextAsyncUnsafe(string value)
    {
      new Thread(() => SetText(value, false)).Start();
    }
    
    private void SetTextAsyncSafe(string value)
    {
      new Thread(() => SetText(value, true)).Start();
    }
    
    private void SetText(string value, bool checkInvokeRequired)
    {
      if (checkInvokeRequired) 
      {
        if (InvokeRequired) 
        {
          Invoke(new Action(() => SetText(value, checkInvokeRequired)));
          return; // early exit
        }
      }
    
      textBox1.Text = value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 487k
  • Answers 487k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer turns out it had something to do with xslt processor,… May 16, 2026 at 8:20 am
  • Editorial Team
    Editorial Team added an answer Your assumption seems correct to me. Did you put the… May 16, 2026 at 8:20 am
  • Editorial Team
    Editorial Team added an answer Wow, that was weird. Your script broke AppleScript Editor. After… May 16, 2026 at 8:20 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Related Questions

No related questions found

Top Members

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.