Eccezione non gestita: System.AggregateException: Si sono verificati uno o più errori. ---> System.IO.IOException: Il file "C:\SyncTemp\Position\Taxi\2017\09\09\Time\0000.dat" è utilizzato da un altro processo, quindi questo file non può essere accessibile. at System.IO.__Error.WinIOError(Int32 errorCode, Stringa maybeFullPath) In System.IO.FileStream.Init(String path, modalità FileMode, FileAccess access, diritti Int32, Boolean useRights, condivisione Fileshare, Int32 bufferSize, opzioni FileOptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) In System.IO.FileStream: ctor(String path, modalità FileMode, accesso FileAccess, condivisione FileShare) In System.IO.File.Open(Percorso stringhe, modalità FileMode, accesso FileAccess) A ConsoleApp1.FileReadHelper.SingleFile(String path) posizione C:\project\my\TestNet\ConsoleApp1\FileReadHelper.cs: riga numero 27 In ConsoleApp1.FileReadHelper.<>c__DisplayClass3_0. <MultipleFilesAsync>b__0(Stringa x) Posizione C:\project\my\TestNet\ConsoleApp1\FileReadHelper.cs: Linea numero 68 In System.Threading.Tasks.Parallel.<>c__DisplayClass17_0'1. <ForWorker>b__1() In System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask) In System.Threading.Tasks.Task.<>c__DisplayClass176_0. <ExecuteSelfReplicating>b__0(Oggetto) --- Fine della traccia interna dello stack eccezioni --- In System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) In System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken) In 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) In 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) su System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable'1 source, Action'1 body) In ConsoleApp1.FileReadHelper.MultipleFilesAsync(IEnumerable'1 path) alla posizione C:\project\my\TestNet\ConsoleApp1\FileReadHelper.cs: linea numero 66 A ConsoleApp1.Program.Main(String[] args) posizione C:\project\my\TestNet\ConsoleApp1\Program.cs: numero riga 73 Per favore, premi qualsiasi tasto per continuare...
L'errore è evidente, cioè il file non può essere accessibile, il file non può essere letto, il che rappresenta un'eccezione nel multithreading
Ho letto come è scritto il documento:
Ho impostato solo la modalità lettura, non la modalità scrittura, e ho scoperto che sotto multithreading c'è comunque un errore, non è solo lettura?
Se è in sola lettura, non dovrebbe essere un errore
Soluzione:
Questo dovrebbe essere la lettura dei file in modalità sola lettura, l'ho testato e ha scoperto che non è un errore, e lo stesso file può essere letto contemporaneamente su più thread
|