This article is a mirror article of machine translation, please click here to jump to the original article.

View: 23060|Reply: 1

[Source] .net/c# multithreaded read-only file solution

[Copy link]
Posted on 10/11/2017 3:06:48 PM | | | |
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




Previous:One-click reinstallation tutorial installation assistant One-click reinstallation guide of system assistant
Next:.net/c# file read operation to read all rows of the file in batches
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com