UI freezes when running program for searching files in whole system using C# Tasks -
UI freezes when running program for searching files in whole system using C# Tasks -
i have written little programme searches kind of files in whole syatem , after search finish shows result in list view when run programme form freezes , can not anything.
can please help in trying prepare problem , below code have written
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.threading.tasks; using system.io; using system.threading; using system.io.compression; using system.security.accesscontrol; namespace searchandziputility { public partial class mainform : form { private string _destfolder; private string _sourcetosearch = @"e:\new books"; private taskscheduler schedular = taskscheduler.current; private cancellationtokensource _ct = new cancellationtokensource(); private list<string> files = new list<string>(); private string _selectedfiletype; public mainform() { initializecomponent(); toolstripstatusinfo.visible = false; btnzip.enabled = false; } protected override void onload(eventargs e) { // bind file types combo box populatecombolist(); if (cmbfiletypes.items.count > 0) { cmbfiletypes.selectedindex = 0; } } private void populatecombolist() { cmbfiletypes.items.addrange(filetypes.getfiletypes()); } private void btndest_click(object sender, eventargs e) { dialogresult dr = folderbrowserdialog.showdialog(); if (dr == system.windows.forms.dialogresult.ok) { _destfolder = folderbrowserdialog.selectedpath; txtdestfolder.text = _destfolder; } } private void btnstartsearch_click(object sender, eventargs e) { toolstripstatusinfo.visible = true; btnstartsearch.enabled = false; filelistview.items.clear(); filelistview.refresh(); _selectedfiletype = cmbfiletypes.selecteditem.tostring(); list<task> tasklist = new list<task>(); driveinfo[] drives = driveinfo.getdrives().where(drive => drive.drivetype == drivetype.fixed).toarray(); seek { foreach (driveinfo d in drives) { driveinfo dinfo = d; //task searchtask = task.factory.startnew( () => { fi}, _ct.token, taskcreationoptions.longrunning, taskscheduler.fromcurrentsynchronizationcontext()); task searchtask = task.factory.startnew(() => { findfiles(dinfo.rootdirectory, ref files); } , _ct.token, taskcreationoptions.longrunning , taskscheduler.fromcurrentsynchronizationcontext()); system.diagnostics.trace.writeline("currently reading" + d.rootdirectory.fullname); tasklist.add(searchtask); searchtask.continuewith(populateresultlist, taskscheduler.fromcurrentsynchronizationcontext()); } task.factory.continuewhenall(tasklist.toarray(), onsearchcompleted, _ct.token, taskcontinuationoptions.none, taskscheduler.fromcurrentsynchronizationcontext()); } grab (exception ex) { system.diagnostics.trace.writeline(ex.message); } { } } private void onsearchcompleted(task[] tasks) { // hide notifier label toolstripstatusinfo.visible = false; btnstartsearch.enabled = true; btnzip.enabled = true; toolstripstatuslabel.text = string.format("total files found: {0}", files.count); } private list<string> searchfiles() { // directoryinfo dirsource = new directoryinfo(_sourcetosearch); _selectedfiletype = cmbfiletypes.selecteditem.tostring(); homecoming files; } private void findfiles(directoryinfo directoryinfo, ref list<string> files) { filesystemaccessrule rule = new filesystemaccessrule(system.security.principal.windowsidentity.getcurrent().name, filesystemrights.fullcontrol, system.security.accesscontrol.accesscontroltype.allow); seek { foreach (directoryinfo d in directoryinfo.getdirectories()) { //d.getaccesscontrol().resetaccessrule(rule); system.diagnostics.trace.writeline("currently reading> " + d.fullname); findfiles(d, ref files); } files.addrange(directoryinfo.getfiles(string.format("*.{0}", _selectedfiletype), searchoption.alldirectories).select(finfo => finfo.fullname)); } grab (unauthorizedaccessexception excep) { system.diagnostics.trace.writeline(excep.message); } grab (exception e) { return; // messagebox.show(e.message); } } private void populateresultlist(task searchedtask) { // fill list view filelistview.items.addrange(files.select(filename => new listviewitem { checked = true, text = filename }).toarray()); toolstripstatuslabel.text = string.format("total files found far: {0}", files.count); } private void btnzip_click(object sender, eventargs e) { } } }
you starting tasks using current task scheduler, in case using message loop.
as such, when message loop starts processing task, it'll block until completed.
try avoid specifying task scheduler, , should spin new threads tasks.
c#
Comments
Post a Comment