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

  • Home
  • SEARCH
  • 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 7500281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:07:31+00:00 2026-05-29T20:07:31+00:00

Something happens in my system on a worker thread. Let’s say that the state

  • 0

Something happens in my system on a worker thread. Let’s say that the state changes. I want to handle the new state in the UI thread, so I dispatch a delegate to be invoked there:

var state = GetState();
Dispatcher.BeginInvoke(() => StateChanged(state));

When StateChanged executes on the UI thread, can I then be sure that the value of the parameter state is the value returned by GetState() before the dispatch, or will the temporary state variable be optimized away so that GetState() is called on the UI thread to populate the StateChanged parameter?

  • 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-29T20:07:34+00:00Added an answer on May 29, 2026 at 8:07 pm

    can I then be sure that the value of the parameter state is the value
    returned by GetState() before the dispatch or will the temporary state variable be optimized > away

    No state won’t be optimized away.
    However, Before is actually the tricky part about this question. As long as you are not changing the state variable after (in the same scope) you have nothing to worry about.

    {
     // say GetState returns 2
     var state = GetState(); 
     // state now = 2
     Dispatcher.BeginInvoke(() => StateChanged(state)); 
     state = 3; 
    }
    

    In the code above state won’t be optimized away. It does not mean, however, that StateChanged will be invoked with value 2. It could be 3, if the worker thread completes execution before starting the dispatcher thread.

    The main point here is that variable capturing insures that the value is preserved for the use of the closure, but that does not mean that the value is immutable.

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

Sidebar

Related Questions

I want that list, because if something horrible happens, and I'll have to reinstall
I have one main parent swf that loads several other swfs. If something happens
So no doubt that building a domain model is something that I think happens
I'm programming a random event system that happens to users when logged in and
I come across several problems while trying django unittests library. Something strange happens: I
What exactly happens, in terms of memory, when i declare something like: char arr[4];
Something that has always bugged me is how unpredictable the setTimeout() method in Javascript
In ASP.NET web application a worker thread creates a non-threadpool thread like the below:
I want to have one model & view that is served by multiple controllers
I want to write to a log every 3 hours using System.IO There is

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.