Multithread system file writing in c#

Asked

Viewed 103 times

0

I am developing a system in which I find myself in the following scenario: a program needs to create a file piece by piece. It receives bytes from a server and so the file is created, it should work this way because it is a project decision.

The problem is that more than one thread can end up recording these bytes, so I think this would cause in a running condition. The way I handled to get around this problem was the implementation of this class:

internal class ConcurrentFile
{
    private readonly object _padlock = new object();
    public string Path { get; set; }

    public ConcurrentFile(string path)
    {
        Path = path;
    }

    public void CreateEmpty()
    {
        lock (_padlock)
        {
            File.Create(Path).Dispose();
        }
    }

    public void Write(byte[] data)
    {
        lock (_padlock)
        {
            using(var fileStream = new FileStream(Path, FileMode.Append, FileAccess.Write, FileShare.Write))
            {
                fileStream.Write(data, 0, data.Length);
                fileStream.Flush();
            }
        }
    }
}

Is this a problematic solution? What problems can I have? By the way, is this code really safe? Help me find a good solution! Thank you for your attention.

  • I see a lot of problems with this code. In practice you’re not even getting the advantages you expect to have, competition is very hard to get right. You know what? Put in Sqlite, will probably have better performance and certainly more robustness.

  • @Maniero, I ran some tests on the code and apparently the lock is controlling the competition. Could you explain what are the types of problems encountered?

  • 2

    If you’ve been tested and you’ve seen that you’re better at competition than I am, there’s no way I can help you. I don’t think you tested as well as you think you do. There are people who are experts on this and can’t fully test.

  • @Maniero, I want to know what were the problems you encountered... Your view on the subject may help in the question in question.

  • @Maniero I appreciate your answer, but could you open an answer detailing the problems? Having this knowledge would be very useful to me, and I also believe it will help other people who might fall for this question with similar problems.

  • Not because the question isn’t about it and if it were, it would be too broad, and it makes the fact that you think you know what you’re doing worse. What I can tell you is that it is better you do with Sqlite, I have been dealing with it for decades and would do so because it is easier to do it right. If you want to learn how to do competition right here is not the right place, you need to learn all the basics to get to knowledge, you need to exercise one thing after another, step by step, without skipping anything, here is for punctual doubts.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.