Forum

C# - safe access fr...
 
Notifications
Clear all

C# - safe access from many threads to one file

1 Posty
1 Users
0 Reactions
1,458 Widok
0
Topic starter

code example

1 Answer
0
Topic starter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        static ReaderWriterLock locker = new ReaderWriterLock();
        public void addToTargetFile(string text)
        {
            locker.AcquireWriterLock(3000);
            try
            {
                using (StreamWriter sw = File.AppendText(target_file))
                {
                    sw.WriteLine(text);
                }
            }
            finally
            {
                locker.ReleaseWriterLock();
            }
        }
        static ReaderWriterLock locker = new ReaderWriterLock();
        public void addToTargetFile(string text)
        {
            locker.AcquireWriterLock(3000);
            try
            {
                using (StreamWriter sw = File.AppendText(target_file))
                {
                    sw.WriteLine(text);
                }
            }
            finally
            {
                locker.ReleaseWriterLock();
            }
        }

Odpowiedź

Author Name

Author Email

Your question *

Preview 0 Revisions Saved
Share: