为什么 CopyFileEx 的 FileUtilities.CopyFile 包装器会干扰 winforms?

发布于 2024-12-19 19:21:14 字数 573 浏览 2 评论 0原文

我在此处使用 CopyFileEx 的 FileUtilities.CopyFile 包装器 http://msdn.microsoft.com/en-us/magazine/cc163851.aspx。我认为 CopyFileCallbackAction 直到文件被复制后才会被调用(我尝试过复制大文件)。因此问这个如何让 CopyFileEx 报告,以便我可以取消文件复制操作? 问题。但现在我发现它实际上被调用了很多次,但由于某种原因它弄乱了我试图显示进度的表单 - 在复制完成之前表单不会更新。事实上,如果我尝试在 Shown 事件处理程序中运行它 - 表单上应该有按钮的空框 - 直到复制完成。这是为什么?

I’m using the FileUtilities.CopyFile wrapper for CopyFileEx from here http://msdn.microsoft.com/en-us/magazine/cc163851.aspx . I thought the CopyFileCallbackAction doesn’t get called until after the file is copied (I’ve tried copying a large file). And therefore asked this How do I get CopyFileEx to report back so I can cancel a file copy operation? question. But now I’ve found that it actually gets called many times, but for some reason it messes the form on which I’m trying to show the progress – the form doesn’t get updated until the copy is done. In fact, if I try running it in the Shown event handler – the form has empty boxes where buttons are supposed to be – until the copy is done. Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筑梦 2024-12-26 19:21:14

您需要从后台线程调用CopyFileEx。目前,对 CopyFileEx 的调用正在阻塞 UI 线程。这就是 UI 不更新的原因。

回调动作确实被重复调用。这样您就可以向用户报告长时间运行的文件操作的进度。

需要明确的是,这就是您调用 CopyFileEx 时发生的情况:

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

在文件复制的整个持续时间内,执行线程忙于复制文件而不是泵送消息队列。尽管这是 WinForms 而不是 Win32,但 WinForms 是标准 Win32 GUI 框架的一个相对轻量级的包装器。您的消息队列需要定期维护,因此所有长时间运行的任务都需要从 UI 线程中运行。

最后一点:请记住,当您获得进度回调时,更新任何 UI 时都需要使用 InvokeBeginInvoke。这是因为更新 UI 的代码需要从 UI 线程运行。

You will need to call CopyFileEx from a background thread. At the moment the call to CopyFileEx is blocking the UI thread. That's why the UI does not update.

The callback action is indeed called repeatedly. This is so that you can report to the user the progress of a long running file operation.

Just to be clear, this is what happens when you call CopyFileEx:

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

For the entire duration of the file copy, the executing thread is busy copying the file rather than pumping the message queue. Although this is WinForms and not Win32, WinForms is a relatively lightweight wrapper around the standard Win32 GUI framework. Your message queue needs to be serviced regularly and so all long running tasks need to be run away from the UI thread.

One final point: remember that when you get your progress callback, you need to use Invoke or BeginInvoke when updating any UI. This is because code that updates UI needs to be run from the UI thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文