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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:04:22+00:00 2026-06-02T05:04:22+00:00

We have an heavily used .Net 3.5 application that reads expensive to create data

  • 0

We have an heavily used .Net 3.5 application that reads “expensive to create” data and caches it. The application reads\writes files based on it not “being used by another process”. If some other process is reading and writing to the file, then the application goes to sleep(for some time) and retries. Is this a correct way of reading and writing files? Please advise.

public void Add<T>(string key, CacheItem<T> item)
        {
            bool fileInUse = false;
            while (!fileInUse)
            {
                try
                {
                    using (Stream stream = new FileStream(Path.Combine(cachePath, key+".bin"), FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        Serializer.NonGeneric.Serialize(stream, item);
                    }
                    fileInUse = true;
                }
                catch (IOException ex)
                {
                    if (ex.Message.Contains("being used by another process"))
                    {
                        //Poll till the file is free to be used by this process
                        Thread.Sleep(100);
                        fileInUse = false;
                    }
                }
            }            
        }        

public CacheItem<T> Get<T>(string key, Type type)
        {
            CacheItem<T> item = null;

            FileInfo fileInfo = new FileInfo(Path.Combine(cachePath, key+".bin"));
            fileInfo.Refresh();
            if (fileInfo.Exists)
            {
                bool fileInUse = false;
                while (!fileInUse)
                {
                    try
                    {
                        using (Stream stream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.None))
                        {
                            object objectTemp = Serializer.NonGeneric.Deserialize(type, stream);
                            item = (CacheItem<T>)objectTemp;
                        }
                        fileInUse = true;
                    }
                    catch(IOException ex) 
                    {
                        if (ex.Message.Contains("being used by another process"))
                        {
                            //Poll till the file is free to be used by this process
                            Thread.Sleep(100);
                            fileInUse = false;
                        }
                    }
                }               
            }                       
            return item;                                   
        }
  • 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-02T05:04:25+00:00Added an answer on June 2, 2026 at 5:04 am

    You could add a global mutex on top of it, to avoid waiting more than absolutely necessary.

    Global mutex is created by passing a non-empty name to Mutex Constructor.

    Benefits:

    • Mutex will allow you to wake up immediately after the file becomes available, instead of waiting 50 ms on average.
    • Mutex allows you to fall asleep once and wake up once, instead of repeatedly sleeping/waking. Threads that are sleeping are handled very efficiently by the OS and consume almost no resources at all.
    • Once you acquire the mutex, the file open is almost 100% likely to succeed, instead of failing potentially many times before succeeding.

    All in all, you are not just going to be faster, you’ll likely consume less CPU cycles in the process.

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

Sidebar

Related Questions

I have an asp.net web application (c#) in visual studio 2010 that is heavily
I have this c# web app that relies heavily on two dll files that
We have an existing application that relies heavily on stored procedures and untyped DataSets.
I have written a PHP application that uses Objects heavily. Added, deleting, updating etc..
I have a pretty big web application that I created last year using ASP.NET
I have a heavily parallelized build across 45 slaves (one master that just handles
I have code that relies heavily on yaml for cross-language serialization and while working
I have an iPhone app that heavily relies on the OpenCV library; as such,
Background: I have a PostgreSQL (v8.3) database that is heavily optimized for OLTP. I
Summary I have written a process monitor command-line application that takes as parameters: The

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.