WPF loading animation on a separate UI thread? (C#) -
WPF loading animation on a separate UI thread? (C#) -
okay, have loading animation runs while big datatable populated allow user know programme has not frozen. have animation working fine, freezes while datatable updatingv well. there way have multiple ui threads, animation go on run while datatable loading information?
edit: current code below.
private void createfiletable() { file_data = new dataset(); data_table = new datatable(); file_data.tables.add(data_table); datacolumn tempcol = new datacolumn("file name", typeof(string)); data_table.columns.add(tempcol); tempcol = new datacolumn("ext", typeof(string)); data_table.columns.add(tempcol); tempcol = new datacolumn("size", typeof(string)); data_table.columns.add(tempcol); tempcol = new datacolumn("created", typeof(label)); data_table.columns.add(tempcol); tempcol = new datacolumn("modified", typeof(label)); data_table.columns.add(tempcol); tempcol = new datacolumn("accessed", typeof(label)); data_table.columns.add(tempcol); tempcol = new datacolumn("location", typeof(string)); data_table.columns.add(tempcol); file_list.itemssource = file_data.tables[0].defaultview; } private void populatedirectories(string[] directories) { (int = 0; < directories.length; i++) { directoryinfo tempdirinfo = new directoryinfo(directories[i]); bool issystem = ((tempdirinfo.attributes & fileattributes.system) == fileattributes.system); if (!issystem) { datarow temprow = data_table.newrow(); temprow["file name"] = tempdirinfo.name; temprow["ext"] = ""; temprow["size"] = ""; templabel = new label(); templabel.padding = new thickness(2, 0, 2, 0); templabel.content = tempdirinfo.creationtime.tolongdatestring() + ", " + tempdirinfo.creationtime.tolongtimestring(); temprow["created"] = templabel; templabel = new label(); templabel.padding = new thickness(2, 0, 2, 0); templabel.content = tempdirinfo.lastwritetime.tolongdatestring() + ", " + tempdirinfo.lastwritetime.tolongtimestring(); temprow["modified"] = templabel; templabel = new label(); templabel.padding = new thickness(2, 0, 2, 0); templabel.content = tempdirinfo.lastaccesstime.tolongdatestring() + ", " + tempdirinfo.lastaccesstime.tolongtimestring(); temprow["accessed"] = templabel; temprow["location"] = tempdirinfo.fullname; data_table.rows.add(temprow); } } } private void populatefiles(string[] files) { (int = 0; < files.length; i++) { fileinfo tempfileinfo = new fileinfo(files[i]); bool issystem = ((file.getattributes(files[i]) & fileattributes.system) == fileattributes.system); if (!issystem) { datarow temprow = data_table.newrow(); temprow["file name"] = tempfileinfo.name; temprow["ext"] = tempfileinfo.extension; int filesize = (int)tempfileinfo.length; if (filesize > 1048576) { temprow["size"] = "" + filesize / 1048576 + " mb"; } else if (filesize > 1024) { temprow["size"] = "" + filesize / 1024 + " kb"; } else { temprow["size"] = "" + filesize + " b"; } templabel = new label(); templabel.padding = new thickness(2, 0, 2, 0); templabel.content = tempfileinfo.creationtime.tolongdatestring() + ", " + tempfileinfo.creationtime.tolongtimestring(); temprow["created"] = templabel; templabel = new label(); templabel.padding = new thickness(2, 0, 2, 0); templabel.content = tempfileinfo.lastwritetime.tolongdatestring() + ", " + tempfileinfo.lastwritetime.tolongtimestring(); temprow["modified"] = templabel; templabel = new label(); templabel.padding = new thickness(2, 0, 2, 0); templabel.content = tempfileinfo.lastaccesstime.tolongdatestring() + ", " + tempfileinfo.lastaccesstime.tolongtimestring(); temprow["accessed"] = templabel; temprow["location"] = tempfileinfo.directoryname; data_table.rows.add(temprow); } } } private string getselectedpath(treeviewitem selectednode) { homecoming selectednode.tag string; } private void populatefilelist() { populatedirectories(directory.getdirectories(getselectedpath((treeviewitem)dir_tree.selecteditem))); populatefiles(directory.getfiles(getselectedpath((treeviewitem)dir_tree.selecteditem))); } private void updatefilelist() { loadingwheel.visibility = system.windows.visibility.visible; createfiletable(); populatefilelist(); txtfoundcount.text = "files/folders found: " + file_list.items.count; loadingwheel.visibility = system.windows.visibility.hidden; }
i have tried using backgroundworker , calling updatefilelist() method within it, i'm not having luck.
edit: below code backgroundworker.
private backgroundworker bgworker1; private void initializebackgroundworker() { bgworker1 = new backgroundworker(); bgworker1.dowork += new doworkeventhandler(bgworker1_dowork); bgworker1.runworkercompleted += new runworkercompletedeventhandler(bgworker1_runworkercompleted); } private void bgworker1_dowork(object sender, doworkeventargs e) { if (dispatcher.checkaccess()) { populatefilelist(); } else { dispatcher.invoke(new action(() => populatefilelist())); } } private void bgworker1_runworkercompleted(object sender, runworkercompletedeventargs e) { txtfoundcount.text = "files/folders found: " + file_list.items.count; loadingwheel.visibility = system.windows.visibility.hidden; } private void updatefilelist() { loadingwheel.visibility = system.windows.visibility.visible; createfiletable(); bgworker1.runworkerasync(); }
i have selectionchanged event on treeview calls initializebackgroundworker() , updatefilelist(). list loads still, , no errors, loading animation never becomes visible.
any help appreciated.
thanks.
there 1 ui thread. need load info in datatable on different thread.
if want show progress datatable loading along way (either directly, or through progressbar or other mechanism), backgroundworker straight-forward way that.
update: simple background worker example here simple example. adds 100 random numbers collection, pausing thread short time between each simulate long loading process. can cutting , paste test project of own see work.
the thing notice heavy lifting (the stuff takes while) done in dowork, while ui updates done in progresschanged , runworkercompleted. in fact, separate list (numbers) created in dowork handler because global mnumbers collection on ui thread, , can't interact in dowork handler.
xaml
<button x:name="btngeneratenumbers" grid.row="1" horizontalalignment="center" verticalalignment="center" content="generate numbers" />
c# code-behind
backgroundworker bgworker = new backgroundworker(); observablecollection<int> mnumbers = new observablecollection<int>(); public window1() { initializecomponent(); bgworker.dowork += new doworkeventhandler(bgworker_dowork); bgworker.progresschanged += new progresschangedeventhandler(bgworker_progresschanged); bgworker.runworkercompleted += new runworkercompletedeventhandler(bgworker_runworkercompleted); bgworker.workerreportsprogress = true; btngeneratenumbers.click += (s, e) => updatenumbers(); this.datacontext = this; } void bgworker_runworkercompleted(object sender, runworkercompletedeventargs e) { progress.visibility = visibility.collapsed; lstitems.opacity = 1d; btngeneratenumbers.isenabled = true; } void bgworker_progresschanged(object sender, progresschangedeventargs e) { list<int> numbers = (list<int>)e.userstate; foreach (int number in numbers) { mnumbers.add(number); } progress.value = e.progresspercentage; } void bgworker_dowork(object sender, doworkeventargs e) { random rnd = new random(); list<int> numbers = new list<int>(10); (int = 1; <= 100; i++) { // add together random number numbers.add(rnd.next()); // sleep 1/8 of sec 1 sec thread.sleep(rnd.next(125, 1000)); // every 10 iterations, study progress if ((i % 10) == 0) { bgworker.reportprogress(i, numbers.tolist<int>()); numbers.clear(); } } } public observablecollection<int> numberitems { { homecoming mnumbers; } } private void updatenumbers() { btngeneratenumbers.isenabled = false; mnumbers.clear(); progress.value = 0; progress.visibility = visibility.visible; lstitems.opacity = 0.5; bgworker.runworkerasync(); }
c# wpf multithreading animation loading
Comments
Post a Comment