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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:09:37+00:00 2026-05-12T22:09:37+00:00

If I have Thread A which is the main Application Thread and a secondary

  • 0

If I have Thread A which is the main Application Thread and a secondary Thread. How can I check if a function is being called within Thread B?

Basically I am trying to implement the following code snippit:

public void ensureRunningOnCorrectThread()
{
    if( function is being called within ThreadB )
    {
         performIO()
    }
    else
    {
         // call performIO so that it is called (invoked?) on ThreadB
    }
}

Is there a way to perform this functionality within C# or is there a better way of looking at the problem?

EDIT 1

I have noticed the following within the MSDN documentation, although Im a dit dubious as to whether or not its a good thing to be doing! :

// if function is being called within ThreadB
if( System.Threading.Thread.CurrentThread.Equals(ThreadB) )
{

}

EDIT 2

I realise that Im looking at this problem in the wrong way (thanks to the answers below who helped me see this) all I care about is that the IO does not happen on ThreadA. This means that it could happen on ThreadB or indeed anyother Thread e.g. a BackgroundWorker. I have decided that creating a new BackgroundWorker within the else portion of the above f statement ensures that the IO is performed in a non-blocking fashion. Im not entirely sure that this is the best solution to my problem, however it appears to work!

  • 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-12T22:09:37+00:00Added an answer on May 12, 2026 at 10:09 pm

    Here’s one way to do it:

    if (System.Threading.Thread.CurrentThread.ManagedThreadId == ThreadB.ManagedThreadId) 
                ...
    

    I don’t know enough about .NET’s Thread class implementation to know if the comparison above is equivalent to Equals() or not, but in absence of this knowledge, comparing the IDs is a safe bet.

    There may be a better (where better = easier, faster, etc.) way to accomplish what you’re trying to do, depending on a few things like:

    • what kind of app (ASP.NET, WinForms, console, etc.) are you building?
    • why do you want to enforce I/O on only one thread?
    • what kind of I/O is this? (e.g. writes to one file? network I/O constrained to one socket? etc.)
    • what are your performance constraints relative to cost of locking, number of concurrent worker threads, etc?
    • whether the “else” clause in your code needs to be blocking, fire-and-forget, or something more sophisticated
    • how you want to deal with timeouts, deadlocks, etc.

    Adding this info to your question would be helpful, although if yours is a WinForms app and you’re talking about user-facing GUI I/O, you can skip the other questions since the scenario is obvious.

    Keep in mind that // call performIO so that it is called (invoked?) on ThreadB implementation will vary depending on whether this is WinForms, ASP.NET, console, etc.

    If WinForms, check out this CodeProject post for a cool way to handle it. Also see MSDN for how this is usually handled using InvokeRequired.

    If Console or generalized server app (no GUI), you’ll need to figure out how to let the main thread know that it has work waiting– and you may want to consider an alternate implementation which has a I/O worker thread or thread pool which just sits around executing queued I/O requests that you queue to it. Or you might want to consider synchronizing your I/O requests (easier) instead of marshalling calls over to one thread (harder).

    If ASP.NET, you’re probably implementing this in the wrong way. It’s usually more effective to use ASP.NET async pages and/or to (per above) synchronize snchronizing to your I/O using lock{} or another synchronization method.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer &mallocbuff is the address of the named variable mallocbuff. &mallocbuff[0]… May 14, 2026 at 4:59 pm
  • Editorial Team
    Editorial Team added an answer I guess thats because haystack uses the document field for… May 14, 2026 at 4:59 pm
  • Editorial Team
    Editorial Team added an answer Your HTML is invalid. A tr element cannot have a… May 14, 2026 at 4:59 pm

Trending Tags

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

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.