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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:52:24+00:00 2026-05-11T01:52:24+00:00

I have a service that I am rewriting to use threading. I understand that

  • 0

I have a service that I am rewriting to use threading. I understand that state from one thread should not be accessed by another, but I’m a little confused by what constitutes ‘state’. Does that mean any field/property/method outside of the method scope?

Specifically, my service looks something like this:

public class MyService {       private IRepository<MyClass> repository;       private ILogger log;       ...       public void MyMethod()       {         ...         var t = new Thread(MyMethodAsync);         t.Start(someState);       }        //Is this OK???       public void MyMethodAsync(object state)       {           var someState = (MyState)state;           log.Log('Starting');           var someData = repository.GetSomeData(someState.Property);           //process data           log.Log('Done');                   }        //Or should I be doing this:       public void MyMethodAsync2(object state)       {           var someState = (MyState)state;           lock(log){              log.Log('Starting');  }            lock(repository){                       var someData = repository.GetSomeData(someState.Property);}            //process data              lock(log){              log.Log('Done'); }                   } } 
  • 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. 2026-05-11T01:52:24+00:00Added an answer on May 11, 2026 at 1:52 am

    Er…nope, you don’t need to lock resources that are read-only. The purpose of locking them is so that if you need to check the value of a resource before writing it then another resource can’t change the value between your read and your write. i.e.:

    SyncLock MyQueue   If MyQueue.Length = 0 Then     PauseFlag.Reset   End If End SyncLock 

    If we were to check the length of our queue before we set the flag to pause our process queue thread and then another resource were to add an item to the queue, then our process queue thread would sit in a paused state while an item potentially could’ve been added between checking the queue length and setting the pause flag…

    If all resources are only accessing the queue in a read only fashion (not that I could think of a single useful application of a read-only queue) then there’s no need to lock it.

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

Sidebar

Related Questions

I have a service that use the tcp binding and this services allows to
I have a service that should check periodically visibility of status bar, when some
We currently have a SOAP based web service that our in house applications use
I have a service that gets a JPA entity from outside code. In this
I have a Service that sends an Intent to my Activity every 0.1 seconds.
I have a service that I would like it to become single instance, because
I have a service that is downloading a file. When the download is done,
I have .Net service that listens on single port over TCP protocol. Clients connect
I have a service that needs to update the registry every 5 minutes (counteract
I have a service that has a dependency on SQL server express (2005). Every

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.