c# - Thread safe file access -
i have program large number of comparisons. compares specific .dat file saved on local machine large number of other files generated on run-time. right unable perform these comparisons using multiple threads because of many system.accessviolationexception
. i'm assuming because multiple threads trying access same local file @ same time. how can overcome these comparisons multiple threads?
there several possible reasons access violation:
- multiple threads exclusively locking specific .dat file
- your multi-threading buggy in regard multiple threads try read same runtime generated file
- your multi-threading buggy in regard threads try read runtime generated files before have been generated completely
the following solutions exist:
- read .dat file memory once , share data between threads. reduces i/o load
- make sure every runtime generated file compared 1 thread. can achieved thread safe queue contains files need compared , shared between threads.
- make sure runtime generated file gets known reading threads after has been created completely. can achieved creating in different directory on same disk , moving target directory putting file name queue solution 2 after creation completed.
as matthew watson correctly points out, accessviolationexception
caused errors in unmanaged code, not caused multiple threads trying access same file.
answer therefore assumes getting unauthorizedaccessexception
.
if not case , indeed getting accessviolationexception
problem lies elsewhere.
Comments
Post a Comment