Why does the FileUtilities.CopyFile wrapper for CopyFileEx interfere with winforms?

Im using the FileUtilities.CopyFile wrapper for CopyFileEx here http://msdn.microsoft.com/en-us/magazine/cc163851.aspx . I thought CopyFileCallbackAction would not be called until the file was copied (Ive tried to copy a large file). And so I asked this How do I get CopyFileEx for a report so that I can cancel the file copy operation? . But now I found that it was actually called many times, but for some reason it messed up the form in which Im trying to show progress - the form is not updated until the copy is executed. In fact, if I try to run it in the Shown event Shown - the form has empty fields where the buttons should be - until the copy is executed. Why is this?

+2
source share
1 answer

You will need to call CopyFileEx from the background thread. Currently, a CopyFileEx call blocks the user interface thread. Therefore, the user interface is not updated.

The callback action is indeed called repeatedly. This means that you can inform the user about the progress with a long file.

To be clear, this happens when you call CopyFileEx :

 Enter CopyFileEx Start copying Call your callback Continue copying Call your callback .... Return from CopyFileEx 

During the entire time the file is copied, the executable stream is busy copying the file, not pumping the message queue. Although these are WinForms, not Win32, WinForms is a relatively lightweight shell around the standard Win32 GUI. Your message queue should be serviced regularly, so all lengthy tasks should be removed from the user interface thread.

One final point: remember that when you get a progress callback, you need to use Invoke or BeginInvoke when updating any user interface. This is because the code updating the interface must be run from the user interface thread.

+9
source

Source: https://habr.com/ru/post/1384789/


All Articles