c# - Why when loading messages form the hard disk using MimeKit it's ver slow? -
i understand when downloading emails server it's slow. when running program , reading/loading messages hard disk there way make faster ?
in constructor start background worker
directoryinfo di = new directoryinfo(emailsdirectory); files = di.getfiles(); if (files.length > 0) { backgroundworker2.runworkerasync(); }
then in dowork event
private void backgroundworker2_dowork(object sender, doworkeventargs e) { int counter = 0; mimekitallloadedmessages = new list<mimekit.mimemessage>(); mimekit.mimemessage loadedmessage = null; directoryinfo di = new directoryinfo(emailsdirectory); fileinfo[] files = di.getfiles(); (int = 0; < files.length; i++) { string uid = seenuids[0]; loadedmessage = mimekit.mimemessage.load(files[i].fullname); mimekitallloadedmessages.add(loadedmessage); downloaded.add(seenuids[i]); counter += 1; int nprogress = counter * 100 / files.length; backgroundworker2.reportprogress(nprogress); } }
then progresschanged event update progressbar pbt1 , listview(listviewcostumcontrol.lvnf)
private void backgroundworker2_progresschanged(object sender, progresschangedeventargs e) { pbt1.value = e.progresspercentage; pbt1.text = e.progresspercentage.tostring() + "%"; pbt1.invalidate(); listviewcostumcontrol.lvnf.items.add(new listviewitem(new string[] { mimekitallloadedmessages[countuploadmsg].from.tostring(), //from column mimekitallloadedmessages[countuploadmsg].subject, //subject column mimekitallloadedmessages[countuploadmsg].date.tostring() //date column })); countuploadmsg += 1; }
and class part of pbt1 maybe make slow ? not sure
public class progressbarwithtext : progressbar { const int wmpaint = 15; sizef textsize; pointf textpos; bool dontpaint = false; public progressbarwithtext() { this.doublebuffered = true; this.textchanged += progressbarwithtext_textchanged; this.sizechanged += progressbarwithtext_sizechanged; } public override string text { { return base.text; } set { base.text = value; } } void recalctextpos() { if (this.isdisposed == true) return; if (string.isnullorempty(base.text)) return; using (var graphics = graphics.fromhwnd(this.handle)) { textsize = graphics.measurestring(base.text, this.font); textpos.x = (this.width / 2) - (textsize.width / 2); textpos.y = (this.height / 2) - (textsize.height / 2); } } void progressbarwithtext_sizechanged(object sender, eventargs e) { recalctextpos(); } void progressbarwithtext_textchanged(object sender, eventargs e) { recalctextpos(); } protected override void wndproc(ref system.windows.forms.message m) { base.wndproc(ref m); if (dontpaint == false) { switch (m.msg) { case wmpaint: using (var graphics = graphics.fromhwnd(handle)) graphics.drawstring(base.text, base.font, brushes.black, textpos.x, textpos.y); break; } } } protected override createparams createparams { { createparams result = base.createparams; result.exstyle |= 0x02000000; // ws_ex_composited return result; } } }
the whole process of loading messages hard disk seems slow. files on hard disk read/load type of eml of files size 8 kb , 380 kb 1 4 mb
what consider "very slow"?
have compared performance of simple program uses mimekit load of messages vs how long takes program progress reporting?
have considered perhaps progress reporting causing slowness? in general, don't want report progress after every small bit of work, want report when progress has changed significantly.
if loading thousands of messages , redraw ui after every message, that's lot of unnecessary re-rendering.
if that's still not fast enough, have considered not loading of messages memory? why not load them when need them? problem might loading many messages memory causing system swap disk.
Comments
Post a Comment