Excepție negestionată: System.AggregateException: Au apărut una sau mai multe erori. ---> System.IO.IOException: Fișierul "C:\SyncTemp\Position\Taxi\2017\09\09\Time\0000.dat" este folosit de un alt proces, astfel încât acest fișier nu poate fi accesat. la System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) În System.IO.FileStream.Init(String path, mod FileMode, FileAccess access, drepturi Int32, Boolean useRights, share FileShare, Int32 bufferSize, opțiuni FileOptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) În System.IO.FileStream: ctor (String path, mod FileMode, acces FileAccess, share FileShare) În System.IO.File.Open(String path, mod FileMode, acces FileAccess) La ConsoleApp1.FileReadHelper.SingleFile(String path) locație C:\project\my\TestNet\ConsoleApp1\FileReadHelper.cs: linia numărul 27 În ConsoleApp1.FileReadHelper.<>c__DisplayClass3_0. <MultipleFilesAsync>b__0(String x) Poziția C:\project\my\TestNet\ConsoleApp1\FileReadHelper.cs: Linia numărul 68 În System.Threading.Tasks.Parallel.<>c__DisplayClass17_0'1. <ForWorker>b__1() În System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask) În System.Threading.Tasks.Task.<>c__DisplayClass176_0. <ExecuteSelfReplicating>b__0(Obiect) --- Sfârșitul traseului intern al stivei de excepții --- În System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) În System.Threading.Tasks.Task.Wait(Int32 milisecondsTimeout, CancellationToken cancellationToken) În System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action'1 body, Action'2 bodyWithState, Func'4 bodyWithLocal, Func'1 localInit, Action'1 localFinally) În System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable'1 source, ParallelOptions, Action'1 body, Action'2 bodyWithState, Action'3 bodyWithStateAndIndex, Func'4 bodyWithStateAndLocal, Func'5 bodyWithEverything, Func'1 localInit, Action'1 localFinally) la System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable'1 sursă, corp Acțiune'1) În ConsoleApp1.FileReadHelper.MultipleFilesAsync(IEnumerable'1 path) la locația C:\project\my\TestNet\ConsoleApp1\FileReadHelper.cs: linia numărul 66 La ConsoleApp1.Program.Main(String[] args) poziția C:\project\my\TestNet\ConsoleApp1\Program.cs: linia numărul 73 Vă rugăm să apăsați orice tastă pentru a continua...
Eroarea este evidentă, adică fișierul nu poate fi accesat, fișierul nu poate fi citit, ceea ce este o excepție aruncată sub multithreading
Am citit cum este scris documentul:
Am setat doar modul de citire, nu și modul de scriere, și am constatat că la multi-threading tot va exista o eroare, nu este doar citire?
Dacă este doar citire, nu ar trebui să fie o eroare
Soluție:
Acesta ar trebui să citească fișiere în modul doar citire, l-am testat și am constatat că nu este o eroare, iar același fișier poate fi citit simultan sub mai multe fire
|