File being used by another process
hello,
i have multi-threaded program several threads can call static method in static class. code use follows:
string connection = null; lock (threadlock) { streamreader sr = null; try { sr = new streamreader(path); connection = sr.readtoend(); sr.close(); sr.dispose(); } catch (exception e) { debug.writeline("there exception accessing file " + path + ":" + e.message); debug.writeline(e.stacktrace); sr.close(); } }
the problem streamreader not seem closing file. following exception:
a first chance exception of type 'system.io.ioexception' occurred in mscorlib.dll there exception accessing file c:\test installation\server\connection.txt:the process cannot access file 'c:\test installation\server\connection.txt' because being used process. @ system.io.__error.winioerror(int32 errorcode, string maybefullpath) @ 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) @ system.io.filestream..ctor(string path, filemode mode, fileaccess access, fileshare share, int32 buffersize, fileoptions options, string msgpath, boolean bfromproxy) @ system.io.filestream..ctor(string path, filemode mode, fileaccess access, fileshare share, int32 buffersize, fileoptions options) @ system.io.streamreader..ctor(string path, encoding encoding, boolean detectencodingfrombyteordermarks, int32 buffersize) @ system.io.streamreader..ctor(string path, boolean detectencodingfrombyteordermarks) @ serverconnection.connectiontools.getconnectionstringfromfile(string path) in c:\vfp\integration\dicomservice\serverconnection\connectiontools.cs:line 33
this not happen, more not. have idea of how can make sure connection broken?
thanks in advance.
do use file reading?
in case may try use filereader class option fileaccess = read , share = system.io.fileshare.readwrite. should not lock file.
http://msdn.microsoft.com/en-us/library/5h0z48dh
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment