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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:33:29+00:00 2026-05-26T06:33:29+00:00

Is it safe to write code in this way? var form = new Form();

  • 0

Is it safe to write code in this way?

        var form = new Form();

        Action callback = 
            () =>
                {
                    // do something 1
                };

        ThreadPool.QueueUserWorkItem(
            args =>
                {
                    // do something 2
                    form.BeginInvoke(callback);
                });

UPD I’m concerned about safety of access to the “form” variable. I use BeginInvoke method from background thread; can I be sure there won’t be any read/write reordering before this moment? (that potentially can leave “form” variable in inconsistent state, from perspective of the background thread)

  • 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-26T06:33:30+00:00Added an answer on May 26, 2026 at 6:33 am

    Yes, it looks OK. The variable form will be captured and as long as it’s not null when the job on the ThreadPool executes it ought to work.

    But you left out a lot of details, I assume this code is all from 1 method.

    // do something 1 can acess the GUI, // do something 2 can not.

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

Sidebar

Related Questions

What's a good way to leverage TDD to drive out thread-safe code? For example,
How safe is it to use Silverlight in production for a graphic form? Is
Below is the part code I currently use to write to a file, Try
Please, may somebody explain me, what can raise an exception in this code? function
I've read the article about scope guards ( Generic: Change the Way You Write
I am calling functions using dynamic function names (something like this) $unsafeFunctionName = $_POST['function_name'];
I am just trying to understand this: multithread is a way of firing a
In the class section of my code, if I write an error a few
Is this operation safe? if not, in order to do the same thing, how
Can anyone point me to a thread-safe implementation of the Copy-on-write (COW) idiom? The

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.