Featured post
c# - Adobe Reader process fails when starting second instance -
in our c# winforms application, generate pdf files , launch adobe reader (or whatever default system .pdf handler is) via process
class. since our pdf files can large (approx 200k), handle exited
event clean temp file afterwards.
the system works required when file opened , closed again. however, when second file opened (before closing adobe reader) second process exits (since reader using it's mdi powers) , in our exited
handler our file.delete call should fail because it's locked joined adobe process. however, in reader instead get:
there error opening document. file cannot found.
the unusual thing if put debugger breakpoint before file deletion , allow attempt (and fail) deletion, system behaves expected!
i'm positive file exists , positive handles/file streams file closed before starting process.
we launching following code:
// open file viewing/printing (if default program supports it) var pdfprocess = new process(); pdfprocess.startinfo.filename = tempfilename; if (pdfprocess.startinfo.verbs.contains("open", stringcomparer.invariantcultureignorecase)) { var verb = pdfprocess.startinfo.verbs.first(v => v.equals("open", stringcomparison.invariantcultureignorecase)); pdfprocess.startinfo.verb = verb; } pdfprocess.startinfo.arguments = "/n"; // specifies new window used! (but not definitely...) pdfprocess.synchronizingobject = this; pdfprocess.enableraisingevents = true; pdfprocess.exited += new eventhandler(pdfprocess_exited); _pdfprocessdictionary.add(pdfprocess, tempfilename); pdfprocess.start();
note: using _pdfprocessdictionary
store references process objects stay in scope exited event can raised.
our cleanup/exited event is:
void pdfprocess_exited(object sender, eventargs e) { debug.assert(!invokerequired); var p = sender process; try { if (_pdfprocessdictionary.containskey(p)) { var tempfilename = _pdfprocessdictionary[p]; if (file.exists(tempfilename)) // how else can check if can delete it!!?? { // note: fail if adobe reader application instance has been re-used! file.delete(tempfilename); _pdfprocessdictionary.remove(p); } cleanotherfiles(); // function clean files other exited processes in our dictionary } } catch (ioexception ex) { // swallow up, deal trying delete @ point } }
possible solutions:
- detect file still open in process
- detect second process hasn't been exited , file opened in first process instead
i just dealt couple of days ago.
when there no instance open already, document opens in new instance directly.
when there instance open, believe instance spawns new instance don't handle to. happens control returns function immediately, goes , deletes file before new instance has had chance read file -- hence appears not there.
i "solved" not deleting files immediately, keeping track of paths in list, , nuking of them when program exits (wrap each delete in try/catch empty catch block in case file has disappeared in meantime).
- Get link
- X
- Other Apps
Comments
Post a Comment