取消按钮单击时取消FileCopy操作

编程入门 行业动态 更新时间:2024-10-09 11:28:41
本文介绍了取消按钮单击时取消FileCopy操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的应用程序有线程,开始点击开始按钮并执行文件复制操作。 OnCancel按钮单击导致线程停止处理并删除复制的文件。 我怎么能实现这个目标?

My application have thread which get started on start button click and do file copy operation. OnCancel button click causes thread to stop the processing and delete copied file. how Can I achieve this?

推荐答案

你可以用很少的逻辑重写你的代码: 阅读以小块文件并将其写入目的地。定期检查是否有人要求取消,如果检测到,请停止写入并关闭文件。 You can rewrite your code with little logic: Read the file in small chunks and write it out to the destination. Periodically check whether you've been asked to cancel and if you detect that, stop writing and close the files.

对于上述逻辑,代码示例附在此处: For the above logic, the code sample is attached here: void CopyStream(Stream inputStream, Stream outputStream) { var buffer = new byte[1024]; int bytesRead; while((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0) { outputStream.Write(buffer, 0, bytesRead); if(cancelled){ // cleanup return; } } }

更多推荐

取消按钮单击时取消FileCopy操作

本文发布于:2023-11-28 01:08:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640366.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   按钮   操作   FileCopy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!