vb.net - IOException issue with asynchronous PDF creation -


i have asynchronous method creates pdf file xml retrieved database. works great, ioexception because when try cleanup temporary .fo file after creating pdf, file still in use.

public sub formatobjecttopdf(byval intrxno integer, byval strsourcefilename string)     dim startinfo new processstartinfo     dim strpdffile string = g_strrootpath & "paperwork\" & intrxno & "m.pdf"      ' if pdf file exists, no need re-create     if not file.exists(strpdffile)         try             startinfo.arguments = "-fo """ & strsourcefilename & """ -pdf """ & strpdffile & """"             startinfo.filename = g_strapppath & "fo.net\fonet.exe"             startinfo.useshellexecute = true             startinfo.windowstyle = processwindowstyle.hidden              using proc process = process.start(startinfo)                 proc.waitforexit()                  if proc.hasexited                     proc.dispose()                 end if             end using         catch ex exception             call insertlog("errorlog", "formatobjecttopdf: " & ex.message, ex)             messagebox.show(ex.message, "create pdf", messageboxbuttons.ok, messageboxicon.exclamation)         end try     end if      ' wait 3 seconds allow file released     system.threading.thread.sleep(3000)      ' delete source fo file when processing complete     if file.exists(strsourcefilename)         try             file.delete(strsourcefilename)         catch iex ioexception             call insertlog("errorlog", "could not delete file '" & strsourcefilename & "': " & iex.message, iex)         catch ex exception             call insertlog("errorlog", "error deleting file '" & strsourcefilename & "': " & ex.message, ex)         end try     end if end sub 

the formatobjecttopdf method called method, asyncxmltopdf, ioexception thrown. had thought exception in formatobjecttopdf since deleting .fo file, had added sleep(3000) see if giving few seconds help.

here asyncxmltopdf:

public sub asyncxmltopdf(byval state object)     dim intrxno = state(0)     dim flgprintresult boolean = state(1)     dim strfilename string = g_strapppath & intrxno & ".fo"     dim stroutput string     dim strpdffile string = g_strrootpath & "paperwork\" & intrxno & "m.pdf"      try         if file.exists(strpdffile)             file.delete(strpdffile)         end if          if not file.exists(strpdffile) andalso not file.exists(strfilename)             stroutput = xmltoformatobject(intrxno, g_strapppath & "fo.net\immfo.xsl")             using writer streamwriter = new streamwriter(strfilename)                 writer.write(stroutput)             end using              call formatobjecttopdf(intrxno, strfilename)         end if     catch ex exception         call insertlog("errorlog", "asyncxmltopdf: " & ex.message, ex)     end try end sub 

the part of either method other declaration of strfilename .fo file in formatobjecttopdf , method has catch block ioexception. why exception being caught in asyncxmltopdf?? here actual error message:

3/25/2015 11:15 am: [ioexception] asyncxmltopdf: process cannot access file 'c:\users\<username>\appdata\local\apps\2.0\1m2d4tcb.rej\3lh3jzy2.tqc\<clickonce app>\561964.fo' because being used process.

everything works expected, other occasional orphaned .fo file when exception occurs. have suggestions on how might able find out problem is?

the part of either method ... .fo file in formatobjecttopdf appears asyncxmltopdf try open streamwriter on it.

if there chance other backgroundworker, thread or task working on same set of files, possible same file in use in asyncxmltopdf , formatobjecttopdf. msdn warns of in file.exists entry:

be aware process can potentially file in between time call exists method , perform operation on file, such delete.

in case looks might a coin flip method exception happen in.

if accidental double click start same process twice, possible same file in use in both methods @ same time. add test see if can open file readwrite. given file not supposed exist yet, @ least reason.

some sort of flag prevent more 1 set of jobs starting might final solution.


Popular posts from this blog

html/hta mutiple file in audio player -

debugging - Reference - What does this error mean in PHP? -