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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:22:22+00:00 2026-05-12T20:22:22+00:00

I’m attempting to create a BitTorrent library in C# as a side project, for

  • 0

I’m attempting to create a BitTorrent library in C# as a side project, for fun. However, I’ve run into a design issue that could create problems later on if I don’t address it now.

I currently have a PeerGreeter class, which puts a Socket in a listening state for any peers that try to connect to me, to serve me files in the torrent. When a peer connects, the greeter exchanges handshakes, makes sure everything is valid, and then fires a PeerConnected event with the associated Socket and handshake information as handler arguments.

My Torrent class, which is a representation of a single torrent and all its duties, has two lists of all peers in the swarm (encapsulated in a Peer object), connected and disconnected. When the greeter fires the PeerConnected event, the Torrent instance finds the corresponding Peer in the disconnected list. If it finds one, it moves it to the connected list, and sets a Connection property of type Socket in its instance to the Socket created by the greeter. The property is an auto-property with the access modifiers as: { get; internal set; }

The problem I’m having is that this, as far as I know, isn’t thread-safe. If one thread is working with a connection of a Peer, and then another thread modifies that connection object somehow, or disposes it, it could create issues. I’ve considered setting the Connection property’s setter’s access modifier to private, and having it set in the constructor, but the problem with that is that I need to create a new object to replace the placeholder Peer in the disconnected list to add it to the connected list.

My question is should I stick with having the setter as internal, or is making it private and replacing the placeholder with an entirely new instance a good approach as well?

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

    For thread-safety purposes, making the type as immutable as possible will save you many, many headaches. Make the accessor private (or better yet, mark the field as readonly) and generate new instances when changes need to be made.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

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.