Unhandled exception: System.AggregateException: One or more errors have occurred. ---> System.IO.IOException: File "C:\SyncTemp\Position\Taxi\2017\09\09\Time\0000.dat" is being used by another process, so this file cannot be accessed. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) In System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) In System.IO.FileStream: ctor(String path, FileMode mode, FileAccess access, FileShare share) In System.IO.File.Open(String path, FileMode mode, FileAccess access) At ConsoleApp1.FileReadHelper.SingleFile(String path) location C:\project\my\TestNet\ConsoleApp1\FileReadHelper.cs: line number 27 In ConsoleApp1.FileReadHelper.<>c__DisplayClass3_0. <MultipleFilesAsync>b__0(String x) Position C:\project\my\TestNet\ConsoleApp1\FileReadHelper.cs: Line number 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(Object ) --- End of the internal exception stack trace --- 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) at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable'1 source, Action'1 body) In ConsoleApp1.FileReadHelper.MultipleFilesAsync(IEnumerable'1 path) at location C:\project\my\TestNet\ConsoleApp1\FileReadHelper.cs: line number 66 At ConsoleApp1.Program.Main(String[] args) position C:\project\my\TestNet\ConsoleApp1\Program.cs: line number 73 Please press any key to continue. . . .
The error is obvious, that is, the file cannot be accessed, the file cannot be read, which is an exception thrown under multithreading
I read how the document is written:
I only set the read mode, not the write mode, and found that under multi-threading, there will still be an error, isn't it read-only?
If it is read-only, it should not be an error
Solution:
This should be reading files in read-only mode, tested it, and found that it is not an error, and the same file can be read at the same time under multiple threads
|