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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:26:06+00:00 2026-05-27T23:26:06+00:00

Assume we have 3 processes, they write to a specific file by order, in

  • 0

Assume we have 3 processes, they write to a specific file by order, in some cases when a process wants to write to the file the process closed (In middle of Writing), so access to file for another 2 processes is in trouble, I add a server process that control order of processes and when a process closed there is an event raised, so how can I release sources (file access) in this event when a process closed. Something like File.Release(FilePath);

Edit:

Write to File as Following:

try {
        if(!File.Exists(FilePath))
            throw new Exception("File does not Exist.");

        bool Clear = false;
        using(StreamReader sr = new StreamReader(FilePath)) {
            if(sr.ReadToEnd().Length > 1200)
                Clear = true;
            }

        if(Clear)
            using(StreamWriter sw = new StreamWriter(FilePath, false)) {
                sw.WriteLine(Text);
                sw.Flush();
        } else
            using(StreamWriter sw = new StreamWriter(FilePath, true)) {
                sw.WriteLine(Text);
                sw.Flush();
        }
} catch(Exception ex) {}
  • 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-27T23:26:06+00:00Added an answer on May 27, 2026 at 11:26 pm
    using System.IO;
    
    class Program
    {
        static void Main()
        {
              using (StreamWriter writer = new StreamWriter("important.txt"))
              {
                    writer.Write("Word ");
                    writer.WriteLine("word 2");
                    writer.WriteLine("Line");
              }
        }
    }
    

    This is the example type we meant when saying use , keyword “using”. Never close the streamwriter explicitly nor dispose it explicitly. using does all the magic for you. It disposes when the work is done and when you go out of this using block.

    In a multi-threaded environment if you use close or dispose explicitly, the threads will have problem considering that the thread may just try to open the object while the previous thread just disposed it. you will have problems synchronizing your threads. This is the same problem that is normally faced in server applications which has multiple connections running into database. That’s the reason we need to use it like this with Idisposable.

    C# USING keyword – when and when not to use it?

    This explains more about it.

    Update: multiple clients writing at the same time to a single file is your problem. As said by the people above, no two process can get hold of the same file at the same time in write mode. Either it has to wait if you write large data, this time of waiting is significantly noticeable or you can collect the objects from the clients, queue them and write them actually from the server. Giving write access on a file that is on the server to the client can also be avoided. If your data is serialized , the you could use google protocol buffers for passing the data.

    If you are handling the file from the client and if the client process is terminated mid-way, there will be no write handle to the file now and obviously it will be free for another process to acquire a handle on it with write access.

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

Sidebar

Related Questions

Assume you have some objects which have several fields they can be compared by:
Assume I have a application that stores data,gets data and processes data and stores
Assume I have an executable called 'exe' which spawns a child process. This child
I'm writing a c# program that will launch many child processes. At some time
Assume all the variables have been declared previously... because they have been. The child
I have a script which, given an ID, kicks off some complex processes and
Assume I have a class foo, and wish to use a std::map to store
Assume I have created a compiled re: x = re.compile('^\d+$') Is there a way
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
Assume I have an ASP.NET MVC app that's not doing anything too fancy (no

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.