Why i'm getting exception IOExceptiob: The process cannot access the file....because it is being used by another process ?
public list<string> lockedthreads(string filename) { string fname = "http://rotter.net/cgi-bin/forum/dcboard.cgi?az=list&forum=scoops1&mm=3&archive="; uri uri = new uri(fname); string str = ""; using (webclient client = new webclient()) { client.headers.add(httprequestheader.contenttype, "charset=windows-1255"); str = client.downloadstring(fname); client.downloadfileasync(uri, @"c:\temp\scoopstest\scooptest.html"); } filename = @"c:\temp\scoopstest\scooptest.html"; string t1 = ""; list<int> numberoflines = new list<int>(); list<string> lockedthreads = new list<string>(); list<string> lockedlinks = new list<string>(); string[] lines = file.readalllines(filename);
i'm using webclient download html file assign html content string , save hard disk.
then assign file name string called filename.
the exception on line:
string[] lines = file.readalllines(filename);
system.io.ioexception unhandled hresult=-2147024864 message=the process cannot access file 'c:\temp\scoopstest\scooptest.html' because being used process. source=mscorlib stacktrace: @ 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, boolean uselongpath, boolean checkhost) @ system.io.filestream..ctor(string path, filemode mode, fileaccess access, fileshare share, int32 buffersize, fileoptions options, string msgpath, boolean bfromproxy, boolean uselongpath, boolean checkhost) @ system.io.streamreader..ctor(string path, encoding encoding, boolean detectencodingfrombyteordermarks, int32 buffersize, boolean checkhost) @ system.io.streamreader..ctor(string path, encoding encoding) @ system.io.file.internalreadalllines(string path, encoding encoding) @ system.io.file.readalllines(string path) @ scrolllabeltest.listsextractions.lockedthreads(string filename) in e:\scrolllabel\scrolllabel\scrolllabel\listsextractions.cs:line 197 @ scrolllabeltest.listsextractions.filterlockednews(list`1 filtered_lnl, string filename) in e:\scrolllabel\scrolllabel\scrolllabel\listsextractions.cs:line 475 @ scrolllabeltest.listsextractions.ext(string filename) in e:\scrolllabel\scrolllabel\scrolllabel\listsextractions.cs:line 362 @ scrolllabeltest.form1.scrollnews() in e:\scrolllabel\scrolllabel\scrolllabel\form1.cs:line 324 @ scrolllabeltest.form1..ctor() in e:\scrolllabel\scrolllabel\scrolllabel\form1.cs:line 109 @ scrolllabeltest.program.main() in e:\scrolllabel\scrolllabel\scrolllabel\program.cs:line 18 @ system.appdomain._nexecuteassembly(runtimeassembly assembly, string[] args) @ system.appdomain.executeassembly(string assemblyfile, evidence assemblysecurity, string[] args) @ microsoft.visualstudio.hostingprocess.hostproc.runusersassembly() @ system.threading.threadhelper.threadstart_context(object state) @ system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.threading.threadhelper.threadstart() innerexception:
the downloadfileasync method not block calling thread file.readalllines method may executed before file has been downloaded.
if on .net 4.5 can await downloadfiletaskasync method download file asynchronously , wait execute remainder of method until file has been downloaded without blocking thread during meantime:
public async task<list<string>> lockedthreads(string filename) { string fname = "http://rotter.net/cgi-bin/forum/dcboard.cgi?az=list&forum=scoops1&mm=3&archive="; uri uri = new uri(fname); string str = ""; using (webclient client = new webclient()) { client.headers.add(httprequestheader.contenttype, "charset=windows-1255"); str = client.downloadstring(fname); await client.downloadfiletaskasync(uri, @"c:\temp\scoopstest\scooptest.html"); } filename = @"c:\temp\scoopstest\scooptest.html"; string t1 = ""; list<int> numberoflines = new list<int>(); list<string> lockedthreads = new list<string>(); list<string> lockedlinks = new list<string>(); string[] lines = file.readalllines(filename); return ...; }
if not on .net 4.5, should execute remainder of method in event handler downloadfilecompleted of webclient:
string file; public list<string> lockedthreads(string filename) { string fname = "http://rotter.net/cgi-bin/forum/dcboard.cgi?az=list&forum=scoops1&mm=3&archive="; uri uri = new uri(fname); string str = ""; using (webclient client = new webclient()) { client.headers.add(httprequestheader.contenttype, "charset=windows-1255"); str = client.downloadstring(fname); client.downloadfileasync(uri, @"c:\temp\scoopstest\scooptest.html"); client.downloadfilecompleted += client_downloadfilecompleted; } file = @"c:\temp\scoopstest\scooptest.html"; return ....; } void client_downloadfilecompleted(object sender, asynccompletedeventargs e) { string t1 = ""; list<int> numberoflines = new list<int>(); list<string> lockedthreads = new list<string>(); list<string> lockedlinks = new list<string>(); string[] lines = file.readalllines(filename); }
...or use synchronous version of downloadfile method:
public list<string> lockedthreads(string filename) { string fname = "http://rotter.net/cgi-bin/forum/dcboard.cgi?az=list&forum=scoops1&mm=3&archive="; uri uri = new uri(fname); string str = ""; using (webclient client = new webclient()) { client.headers.add(httprequestheader.contenttype, "charset=windows-1255"); str = client.downloadstring(fname); client.downloadfile(uri, @"c:\temp\scoopstest\scooptest.html"); } filename = @"c:\temp\scoopstest\scooptest.html"; string t1 = ""; list<int> numberoflines = new list<int>(); list<string> lockedthreads = new list<string>(); list<string> lockedlinks = new list<string>(); string[] lines = file.readalllines(filename); }
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment