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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:48:00+00:00 2026-05-13T09:48:00+00:00

Suppose I have some code which is running in the UI thread, which spawns

  • 0

Suppose I have some code which is running in the UI thread, which spawns a new thread to do work in the background, and then goes on to do UI work. When the background work is done, BeginInvoke is called from the new thread. If the UI work is still going on, will the callback given to BeginInvoke interrupt the UI work, or will it wait?

Code example – add a button called button1 to a form and add button1_Click to its click event, and then add this to the form:

bool continueWork = true;

private void button1_Click(object sender, EventArgs e)
{
    Thread workThread = new Thread(performBackgroundWork);

    workThread.IsBackground = true;
    workThread.SetApartmentState(ApartmentState.STA);
    workThread.Start();

    for (long i = 0; i < long.MaxValue; i++)
    {
        if (!continueWork)
            break;

        button1.Text = i.ToString();
    }
}

private void performBackgroundWork()
{
    Thread.Sleep(1);
    button1.BeginInvoke(new MethodInvoker(stopUIWork));            
}

private void stopUIWork()
{
    continueWork = false;
}

What is button1’s text after it is clicked?

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

    BeginInvoke adds the delegate to a queue (the message queue to be exact). So the answer is no, they won’t get interrupted. Your button click handler is in fact executed due to a message in the message queue as well.

    EDIT: Sorry, I forgot to answer your second question, but you can probably figure that out by yourself. The button text will be long.MaxValue – 1. You would probably want to execute stopUIWork without using BeginInvoke.

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

Sidebar

Related Questions

I have some not understanding actions from gnu clisp Suppose, I have some code
Suppose I have a Javascript file function js_main(args){ /* some code */ var x
Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ...
Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
Suppose I have the following code: foreach(string str in someObj.GetMyStrings()) { // do some
Suppose, I have saved some permissions in the database by using this code: RoleRepository
Suppose I have some class which has a property actor_ of type Actor .
Suppose I have some pointer, which I want to reinterpret as static dimension array
I have UI which displaying status of long-running operations (downloading some text files from
Suppose I have some code like this: class Visitor { public: Visitor(callBackFunction) {} void

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.