c# - Handling streams while Batch Printing RDLC reports from a console application -
i printing reports console application. started off using manner prescribed microsoft ( http://msdn.microsoft.com/en-us/library/ms252172(v=vs.100).aspx). in technique, 1 renders report so:
report.render("image", deviceinfo, createstream, out warnings);
where createstream callback function:
private stream createstream(string name, string filenameextension, encoding encoding, string mimetype, bool willseek) { stream stream = new filestream(name + "." + filenameextension, filemode.create); m_streams.add(stream); return stream; }
the idea being render reports want files, leaving streams open , collected in array. once rendered, iterate through array, using open streams send file contents printer.
this works few reports, in case, printing around 1500 1 page reports. obviously, cannot leave streams hanging open without serious degradation performance. so, modified things. created class level stream object (renderstream) use in createstream callback, , save file name in collection.
renderstream = new filestream(filename, filemode.create, fileaccess.write, fileshare.read); renderedfilelist.add(filename);
i renderstream.dispose() after report.render() function returns.
this way streams stay closed , disposed. , after of exports done, when go print, in printpage event this:
private void printpage(object sender, printpageeventargs ev) { using (stream stream = new filestream(renderedfilelist[filecounter], filemode.open, fileaccess.read, fileshare.read)) { metafile pageimage = new metafile(stream); ev.graphics.drawimage(pageimage, ev.pagebounds); } }
now, problem lies. while usually have no problem opening streams rendered files, 5% of attempts access files @ print-time return exceptions, reporting file not accessible because opened process. though prior stream has been disposed.
what can around this?
i face same problem , came out way around. followed approach refering your link..
kept list files created stream:
private list<string> _emffiles = new list<string>();
modified createstream
method to:
private stream createstream(string name, string filenameextension, encoding encoding, string mimetype, bool willseek) { stream stream = new filestream(name + "." + filenameextension, filemode.create); m_streams.add(stream); //get files _emffiles.add(name + "." + filenameextension); return stream; }
so _emffiles
list have emf files created stream. then, in disposed
method;
public void disposed() { if (m_streams != null) { foreach (stream stream in m_streams) stream.close(); m_streans = null; } //delete every file created stream foreach (string file in _emffiles) { if (file.exists(file)) file.delete(file); } }
finally called disposed
method in run
method:
private void run() { localreport report = new localreport(); report.reportpath = "report.rdlc"; report.datasources.add(new reportdatasource("sales", loadsalesdata())); export(report); m_currentpageindex = 0; print(); //after finishing printing delete emf files disposed(); }
this approach works me. hope helps..
Comments
Post a Comment