c# - BackgroundWorker and CPU usage issues? -
i want iterate through files in computer , save filenames text file. in order avoid ui blocking, i'm using inside background worker. code below works fine. however, cpu usage high. sometime it's above 60%
private void filelistmanagementworker_dowork(object sender, doworkeventargs e) { driveinfo[] alldrives = driveinfo.getdrives(); list<string> directorylist = new list<string>(); foreach (driveinfo d in alldrives) { if (d.isready) { applyallfiles(d.name, processfile); } } // iterate on personal folders foreach (environment.specialfolder s in enum.getvalues(typeof(environment.specialfolder))) { string pth = environment.getfolderpath(s); applyallfiles(pth, processfile); } } private void applyallfiles(string folder, action<string> fileaction) { try { foreach (string file in directory.getfiles(folder)) { fileaction(file); } foreach (string subdir in directory.getdirectories(folder)) { try { applyallfiles(subdir, fileaction); } catch { // swallow, log, whatever } } } catch (exception e) { } } private void processfile(string path) { //messagebox.show("1"); // logic path = path.replace(":",""); path = path.replace("\\", "/"); // messagebox.show(path.replace("\\", "/")); if (!path.contains("recycle")) { try { file.appendalltext("allfileslist.txt", path + "@!@"); } catch (exception ed) { // messagebox.show(ed.message); } } }
question : how reduce cpu usage. it's above 60%
there's nothing wrong background process using 100% of cpu, long doesn't block else needs it.
for example in windows, when process using 100% cpu , press ctrl+alt+delete, should take priority. if shows lock screen (almost) there's no problem.
if doesn't until process has finished have big problem , that's when need investigate. otherwise don't worry it!
Comments
Post a Comment