DO NOT DEAL WITH THIS MEMBER
Nov 12, 2019 08:16 PM
(This post was last modified: Nov 12, 2019 08:16 PM by dor343232.)
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:37 PM
(This post was last modified: Nov 12, 2019 09:12 PM by Eternal Darkness.)
(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.
DO NOT DEAL WITH THIS MEMBER
(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 10:16 PM
(This post was last modified: Nov 12, 2019 10:20 PM by Eternal Darkness.)
(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.
DO NOT DEAL WITH THIS MEMBER
Nov 12, 2019 11:13 PM
(This post was last modified: Nov 12, 2019 11:14 PM by dor343232.)
(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 13, 2019 12:16 AM
(This post was last modified: Nov 13, 2019 12:16 AM by Eternal Darkness.)
(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.
DO NOT DEAL WITH THIS MEMBER
(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 !
|