.net - C# Updating Images on button click in new Thread -
i have list of image tags in xaml wish update 1 after other , sleep inbetween.
i have following code, update @ once , ui remains frozen until finished.
please can me make update 'live' image sources set?
here's have far:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading; using system.threading.tasks; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes; namespace jobmonitor { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { // add test lights list private readonly dictionary<int, image> imagedictionary; public mainwindow() { initializecomponent(); imagedictionary = new dictionary<int, image> { {1, testlight1}, {2, testlight2}, {3, testlight3}, {4, testlight4}, {5, testlight5}, {6, testlight6}, }; } private void button_click(object sender, routedeventargs e) { dispatcher.invokeasync(changeimage); } private void changeimage() { // loop through each of tests foreach (var testlight in imagedictionary) { changeimagelights(testlight.value); thread.sleep(1000); } } private void changeimagelights(image img) { var myimage3 = new image(); var redlightimage = new bitmapimage(); redlightimage.begininit(); redlightimage.urisource = new uri("red_light.png", urikind.relative); redlightimage.endinit(); myimage3.stretch = stretch.fill; myimage3.source = redlightimage; img.source = redlightimage; } } } xaml:
<image margin="0,0,20,0" height="40" source="green_light.png" stretch="fill" name="testlight1" /> <image margin="0,0,20,0" height="40" source="green_light.png" stretch="fill" name="testlight2" /> <image margin="0,0,20,0" height="40" source="green_light.png" stretch="fill" name="testlight3" /> <image margin="0,0,20,0" height="40" source="green_light.png" stretch="fill" name="testlight4" /> <image margin="0,0,20,0" height="40" source="green_light.png" stretch="fill" name="testlight5" /> <image margin="0,0,20,0" height="40" source="green_light.png" stretch="fill" name="testlight6" /> i thought using dispatcher.invokeasync() answers troubles why have put in there....
you can deduce invokeasync isn't running on thread because you're allowed perform operations in changeimagelights. 1 approach leverage backgroundworker:
// new private class variable private backgroundworker _worker = new backgroundworker(); // constructor code public .ctor() { _worker.workerreportsprogress = true; _worker.dowork += (s, e) => { // loop through each of tests foreach (var testlight in imagedictionary) { _worker.reportprogress(1, testlight.value); thread.sleep(1000); } } _worker.progresschanged += (s, e) => { var myimage3 = new image(); var redlightimage = new bitmapimage(); redlightimage.begininit(); redlightimage.urisource = new uri("red_light.png", urikind.relative); redlightimage.endinit(); myimage3.stretch = stretch.fill; myimage3.source = redlightimage; ((image)e.userstate).source = redlightimage; } } the backgroundworker starts new thread , runs dowork handler on thread. when call reportprogress handles switching threads can modify ui in progresschanged handler.
now background thread sleep 1 second before reports more progress.
Comments
Post a Comment