![]() |
|
[C#] Thread write to Fille, is wrtie Duplicate strings - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] Thread write to Fille, is wrtie Duplicate strings (/showthread.php?tid=6024507) |
Thread write to Fille, is wrtie Duplicate strings - dor343232 - Nov 12, 2019 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 š¤ š¤ š¤ RE: Thread write to Fille, is wrtie Duplicate strings - Eternal Darkness - Nov 12, 2019 (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 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/File-locking-in-a-multi-threaded-environment https://codereview.stackexchange.com/a/49161 RE: Thread write to Fille, is wrtie Duplicate strings - dor343232 - Nov 12, 2019 (Nov 12, 2019 08:37 PM)EternalĀ Darkness Wrote: Locking. (Perhaps some more good documentation: readerwriterlock) i am stell new in C# any more Exemple ?? like more easy ? RE: Thread write to Fille, is wrtie Duplicate strings - Eternal Darkness - Nov 12, 2019 (Nov 12, 2019 09:50 PM)dor343232 Wrote: i am stell new in C# any more Exemple ?? Code private object locker = new Object();"Easy" (version of), no error handling/etc (definitely wanted). RE: Thread write to Fille, is wrtie Duplicate strings - dor343232 - Nov 12, 2019 (Nov 12, 2019 10:16 PM)EternalĀ Darkness 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[]) RE: Thread write to Fille, is wrtie Duplicate strings - Eternal Darkness - Nov 13, 2019 (Nov 12, 2019 11:13 PM)dor343232 Wrote: Got errorr 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) RE: Thread write to Fille, is wrtie Duplicate strings - dor343232 - Nov 13, 2019 (Nov 13, 2019 12:16 AM)EternalĀ Darkness Wrote: Got it to work in a min. THX ! |