c# - The main form is frozen after finishing copying file (with a progress indicator) using CopyFileEx? -
i have main form, form has button clicking on show copying window progressbar. use thread copying job, after finishing copying (the file copied ok , copying window closed) main form frozen (the controls on form seem not interactive with). task manager shows there not work (0%). there strange here. here code copying dialogue, please see:
public partial class filecopier : form { [dllimport("kernel32")] private extern static int copyfileex(string source, string destination, copyprogressroutine copyprogress, int data, ref int cancel, int flags); private delegate int copyprogressroutine(long totalbytes, long bytescopied, long streamsize, long streamcopied, int streamnumber, int callbackreason, int source, int destination, int data); public filecopier() { initializecomponent(); } #region private members int cancel; int copyfinished = -1; #endregion #region public methods public void copy(string source, string destination) { corehandling(source, destination); } public void movefile(string source, string destination) { corehandling(source, destination); if (cancel == 0)//if there no canceling action { //delete source file file.delete(source); } } #endregion #region private methods private void corehandling(string source, string destination) { starttime = datetime.now; threadstart ths = delegate { copyfileex(source, destination, updatecopyprogress, 0, ref cancel, 1); }; new thread(ths).start(); showdialog(); while (copyfinished == -1) { thread.sleep(10); } copyfinished = -1; } private int updatecopyprogress(long totalbytes, long bytescopied, long streamsize, long streamcopied, int streamnumber, int callbackreason, int source, int destination, int data) { if (invokerequired) { invoke(new copyprogressroutine(updatecopyprogress), totalbytes, bytescopied, streamsize, streamcopied, streamnumber, callbackreason, source, destination, data); } else { int percentage = (int)(((double)bytescopied / totalbytes) * 100); progressbar.position = percentage; application.doevents(); if (totalbytes == bytescopied || cancel == 1) { dialogresult = dialogresult.ok; } } return 0; } #endregion private void buttoncancel_click(object sender, eventargs e) { cancel = 1; } } in code main form, here button click event handler:
private void buttoncopy_click(object sender, eventargs e){ using(filecopier filecopier = new filecopier()){ filecopier.copy("my source file", "my destination file"); } } that's all, after finishing copying, method copy() above should exit normally. don't understand what's still doing after finishing copying , makes main form frozen.
your highly appreciated!
copyfinished not modified anywhere , main thread sleeps indefinitely
Comments
Post a Comment