Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Thread write to Fille, is wrtie Duplicate strings

i have probelm if i run my multi Thread to write file i get 2 type of errorr

run like 50 Thread or 500 same Problem.

Thread myNewThread = new Thread(() => Start(lines));
myNewThread.Start();

1) System.IO.IOException: The process cannot access the file

2) is write 2 - 5 times the same line ..


how can i fix it 🤝 🤝 🤝
(Nov 12, 2019 08:16 PM)dor343232 Wrote:  i have probelm if i run my multi Thread to write file i get 2 type of errorr

run like 50 Thread or 500 same Problem.

Thread myNewThread = new Thread(() => Start(lines));
myNewThread.Start();

1) System.IO.IOException: The process cannot access the file

2) is write 2 - 5 times the same line ..


how can i fix it  🤝  🤝  🤝

Locking. (Perhaps some more good documentation: readerwriterlock)
Cannot write to a file with more than one operation at a time. Need to make sure that you only write to the file when you have access to the "locked door" (file is not currently being written to in another thread, in this instance).

What may be simpler/easier is to add the output to some Data Structure (Think: List, Array, Queue, etc) and output to file in a single write instead.

Examples with locks:
https://www.codeproject.com/Tips/1190802...nvironment
https://codereview.stackexchange.com/a/49161
I post my opinions, please try not to be offended... I am not saying I am superior, but my interpretation of what you are saying may be befuddling.
(Nov 12, 2019 08:37 PM)Eternal Darkness Wrote:  Locking. (Perhaps some more good documentation: readerwriterlock)
Cannot write to a file with more than one operation at a time. Need to make sure that you only write to the file when you have access to the "locked door" (file is not currently being written to in another thread, in this instance).

What may be simpler/easier is to add the output to some Data Structure (Think: List, Array, Queue, etc) and output to file in a single write instead.

Examples with locks:
File Locking in a Multi Threaded Environment - CodeProject
c# - Writing to file in a thread safe manner - Code Review Stack Exchange

i am stell new in C# any more Exemple ??

like more easy ?
(Nov 12, 2019 09:50 PM)dor343232 Wrote:  i am stell new in C# any more Exemple ??

like more easy ?

Code
private object locker = new Object();
public void WriteToFile(String path, String text) {
    lock (locker) {
        System.IO.File.AppendAllText(path, text); //https://docs.microsoft.com/en-us/dotnet/api/system.io.file.appendalltext?view=netframework-4.8
    }
}
call: WriteToFile(@"C:\...\whateverfile.txt", variable);

"Easy" (version of), no error handling/etc (definitely wanted).
I post my opinions, please try not to be offended... I am not saying I am superior, but my interpretation of what you are saying may be befuddling.
(Nov 12, 2019 10:16 PM)Eternal Darkness Wrote:  
Code
private object locker = new Object();
public void WriteToFile(String path, String text) {
lock (locker) {
System.IO.File.AppendAllText(path, text); //[url=https://#/a8n]File.AppendAllText Method (System.IO) | Microsoft Docs[/url]
}
}
call: WriteToFile(@"C:\...\whateverfile.txt", variable);

"Easy" (version of), no _error_ handling/etc (definitely wanted).

Got errorr

Thread myNewThread = new Thread(() => Start(ArrayLines));
myNewThread.Start();


An object reference is required for the non-static field, method, or property 'Program.StartBrut(string[])
(Nov 12, 2019 11:13 PM)dor343232 Wrote:  Got errorr

Thread myNewThread = new Thread(() => Start(ArrayLines));
myNewThread.Start();


An object reference is required for the non-static field, method, or property 'Program.StartBrut(string[])

Got it to work in a min.

I assume ArrayLines is inaccessible (at wherever you are starting this thread)?
private object locker is a global variable meaning it is outside of any functions/methods (voids). Not sure how it could be my code, other than the global var being misplaced/something...
Want to post your code? Can tell what it is based off method name.
Btw, ideally you should start a thread like: https://stackoverflow.com/a/3360586 (Needs to be object parameter). (Relatively Small Difference)
I post my opinions, please try not to be offended... I am not saying I am superior, but my interpretation of what you are saying may be befuddling.
(Nov 13, 2019 12:16 AM)Eternal Darkness Wrote:  Got it to work in a min.

I assume ArrayLines is inaccessible (at wherever you are starting this thread)?
private object locker is a global variable meaning it is outside of any functions/methods (voids). Not sure how it could be my code, other than the global var being misplaced/something...
Want to post your code? Can tell what it is based off method name.
Btw, ideally you should start a thread like: c# - How to pass parameters to ThreadStart method in Thread? - Stack Overflow (Needs to be object parameter). (Relatively Small Difference)

THX !


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by pro_fesnoobSep 24, 2022 04:24 PM 14 797 Sep 24, 2022 04:24 PM
Last Post: BinaryFox
[C#] by draxyeeApr 28, 2020 10:33 PM 5 228 Apr 28, 2020 10:33 PM
Last Post: Shadow_Cloud
[C#] by dor343232Nov 14, 2019 06:46 PM 23 769 Nov 14, 2019 06:46 PM
Last Post: 0xDEAD10CC