c# - How to not block the UI while doing time consuming work which DOES Involve accessing UI Controls? -
c# - How to not block the UI while doing time consuming work which DOES Involve accessing UI Controls? -
i have method time consuming 1 , hence have been trying implement backgroundworker, not allow accessing ui controls have read , tried (hacking it) no avail.
what method does: creates new bitmapimage
, sets source local or streamed (the parameter), writes new writeablebitmap
, used converttograyscale
, saves bw re-create isolatedstorage
in folder.
so happens quite fast. but, when have less 25 source images. if have 100+ images, takes considerably long 20 seconds or more , therefore, show progressbar in same phoneapplicationpage
have been struggling how not block ui , show progressbar while method doing work.
this code have:
void getimages() { if (!myisolatedstorage.directoryexists("imagesbw") && !_appsettings.contains("_update")) { myisolatedstorage.createdirectory("imagesbw "); (int = 0; < coll.desserts.count; i++) { bitmapimage bmp = new bitmapimage(); bmp.createoptions = bitmapcreateoptions.none; if (coll.desserts[i].hasassociatedimage) { bmp.setsource(coll.desserts[i].getimage()); writeablebitmap wb = new writeablebitmap(bmp); converttograyscale(wb); bitmapimage bit = convertwbtobi(wb); savebwcopy(bi, i.tostring()); } else { bmp.urisource = new uri("/assets/images/missingart.png", urikind.relative); writeablebitmap wb = new writeablebitmap(bmp); converttograyscale(wb); bitmapimage bit = convertwbtobi(wb); savebwcopy(bi, i.tostring()); } } _appsettings["_firstlaunch"] = "false"; _appsettings.save(); } else if (myisolatedstorage.directoryexists("imagesbw ") && _appsettings.contains("_update")) { string[] files = myisolatedstorage.getfilenames("imagesbw/*"); (int s = 0; s < files.length; s++) { myisolatedstorage.deletefile("imagesbw/" + s + ".jpg"); } myisolatedstorage.deletedirectory("imagesbw"); myisolatedstorage.createdirectory("imagesbw"); (int = 0; < coll.desserts.count; i++) { bitmapimage bmp = new bitmapimage(); bmp.createoptions = bitmapcreateoptions.none; if (coll.desserts[i].hasassociatedimage) { bmp.setsource(coll.desserts[i].getimage()); writeablebitmap wb = new writeablebitmap(bmp); converttograyscale(wb); bitmapimage bit = convertwbtobi(wb); savebwcopy(bi, i.tostring()); } else { bmp.urisource = new uri("/assets/images/missingart.png", urikind.relative); writeablebitmap wb = new writeablebitmap(bmp); converttograyscale(wb); bitmapimage bit = convertwbtobi(wb); savebwcopy(bi, i.tostring()); } } _appsettings.remove("_update"); _appsettings.save(); } btnstart.isenabled = true; }
backgroundorker best bet. there huge amount of resources on web how implement it. thought behind backgroundworker runs in separate thread ui. can access main thread ui 2 methods, delegate or through backgroundworker's progresschanged method, gives access ui thread.
lets have this.
public mainwindow() { initializecomponent(); //if want disable button can here backgroundworker _bw = new backgroundworker(); _bw.dowork += new doworkeventhandler(_bw_dowork); _bw.workerreportsprogress = true; _bw.progresschanged += new progresschangedeventhandler(_bw_progresschanged); _bw.runworkercompleted += new runworkercompletedeventhandler(_bw_runworkercompleted); _bw.runworkerasync(); //or here //display progress bar here } void _bw_runworkercompleted(object sender, runworkercompletedeventargs e) { //this can give access ui after work completed // check ok or hide progress bar } void _bw_progresschanged(object sender, progresschangedeventargs e) { //this give access ui in middle of work update progress bar } void _bw_dowork(object sender, doworkeventargs e) { //actual work here including getimages //work work work getimages(); }
in getimages method after savebwcopy can add together update progress bar
_bw.reportprogress(int progress ) //progress percentage want send progress bar display going in e eventargument passed.
c# xaml windows-phone-8 progress-bar ui-thread
Comments
Post a Comment