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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:09:28+00:00 2026-06-10T16:09:28+00:00

I have the following function that was originally being invoked from the UI (Main)

  • 0

I have the following function that was originally being invoked from the UI (Main) thread:

    private void BreakToggle(int line, Boolean toggle)
    {
        string flag;
        if (toggle)
        {
            flag = "0"; //Add
        }
        else
        {
            flag = "1"; //Delete
        }
        string logicLine = line.ToString();

        SetLogicBreakLineResponse response = ddcdao.SetLogicBreakLine(logicName, logicLine, flag);
    }

However, the bottom line tries to establish connection with an electronic device and send a signal, which can take upto 5 seconds if the connection is slow. So to prevent the UI from hanging, I decided to make a separate thread to handle this.

This function can also be called many times in a short period of time (say 10 times within 1 second), so I figured instead of having a backgroundworker to take care of this, I should use Threadpool so I modified my code like so:

 ThreadPool.QueueUserWorkItem(state =>
 {
        SetLogicBreakLineResponse response = ddcdao.SetLogicBreakLine(logicName, logicLine, flag);
 });

Is that the proper way of using Threadpool? I feel like I’m definitely doing something wrong if using threads is this easy. Will this code cause any unknown voodoo to my application?

  • 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-10T16:09:30+00:00Added an answer on June 10, 2026 at 4:09 pm

    Your example is a reasonable start for using the thread pool. It can be as easy as that if the only thing you are interested in is just getting the code to run outside your UI thread.

    However, you should be aware of differences in behavior between your original code and the multithreaded version:

    • The MT version does not guarantee in which order queued operations will run
    • The MT version allows for the possibility of multiple operations running at once, which means SetLogicBreakLine needs to be reentrant
    • Using closures to capture the values of reference types allows for the possibility of their members being mutated in after you schedule them and before they get to run

    You do need to think about these differences, but if there’s nothing wrong with the MT behavior then it’s simply as easy as that.

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

Sidebar

Related Questions

I have the following function that retrieves an image from a twitter feed, the
I have the following function that works properly in Chrome and Firefox, but not
I have the following function that does string splitting: on splitText(aString, delimiter) set retVal
I have the following function that deletes the LaTeX command surrounding the current cursor
I have the following function that I am using to remove the characters \04
I have the following function that takes a number like 5 and creates a
I have the following function that is supposed to trigger anytime one of the
I have the following function that I only want to run inside <div id=imgWrapper>
I have the following jquery function that submits to an iframe. The message sent
I have the following structure and function that adds things to the structure: struct

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.