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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:37:56+00:00 2026-05-22T19:37:56+00:00

This problem is encountered in part of a complicated system where many threads need

  • 0

This problem is encountered in part of a complicated system where many threads need to access to the same method. This project is written in C#. To simplify the problem, I use the following illustration:

I have two customised classes, the parent class of these two classes is the same. The two classes are Apple and Orange, the parent is Fruit. The problem came when I expect an apple, but it is actually an Orange. This happens sometimes, not not a rare event, probably 10% of the time.

public void MethodAlpha(object food)
{
   lock(this)
   {
       FoodObj foodObj = Converter.XMLStringToObject(food, ...) //"food" is a string

       if(foodObj.FoodType == StringConstants.Apple)
       {
           Apple apple = (Apple)foodObj.FoodObject; <--InvaildCastException
           ...
       }
       ...
   }
}

The InvaildCastException says I am trying to cast an Orange to an Apple, when I have already handled it by “lock(this)” and the if checking. Have I handled it incorrectly? How should I resolve this issue?

  • 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-22T19:37:57+00:00Added an answer on May 22, 2026 at 7:37 pm

    you say food is a string; now, strings are immutable, so that hasn’t changed during the method – and the parameter food is not by-ref (ref), so that hasn’t changed during the method.

    Converter.XMLStringToObject appears (hard to say) to parse the string to an object, so there is no shared state there unless you have some cache that you haven’t told us about.

    So… this is just a parsing bug in your code; nothing to do with threading.

    Note: method parameters and variables are per call – they are isolated (unless they refer to shared state).

    This is not a threading issue. Just a run-of-the-mill bug. At some point during parsing, you’ve called it an apple, but assigned it an orange. Trace through XMLStringToObject to find it, looking in particular at where .FoodType and FoodObject get assigned.

    As an aside – lock(this) is a bad idea in general; in this case, it also does nothing useful since you have no shared state to protect. If there was a use, having a dedicated object for this would be preferred:

    private readonly object syncLock = new object();
    ...
    lock(syncLock) {...}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Somehow this is the first time I've ever encountered this problem in many years
I've encountered this problem (while trying to add SQL Server Database (.mdf) file to
Hi i encountered this problem whereby when i initialized my String[], there seems to
Uhm I'm not sure if anyone has encountered this problem a brief description is
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
This problem involved me not knowing enough of C++. I am trying to access
I've encountered somewhat of a problem in MEF's part lifetime which causes memory leaks
Okay, so this is really werid. I've never encountered anything like this. Part of
I encountered this problem using a third party library provided by a different group
As part of a very slow refactoring process of an inherited system, I need

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.