I have a background worker that works to copy a huge file (several GB), and I would like to know how to cancel the process in the middle of the copy. I can check the CancellationPending property before the copy, but I don’t know how to do it when the copy is already running.
if (worker.CancellationPending) // check cancellation before copy { e.Cancel = true; } else { File.Copy("sourceFile", "destinationFile"); // need to cancel this }
Please advise, thanks!
I am not sure, but I think it is File.Copysupported by the CopyFilewinapi function , which does not allow this function.
File.Copy
CopyFile
You must point to CopyFileExwhich allows you to use the callback method whenever a part of the file has been copied.
CopyFileEx
, , - CopyFileEx (kernel32)
, . , , . , , . ( , , , , .)
. , .
, , dllimport CopyFileEx.
Instead of using File.Copyor any other copy function, you can also copy the file yourself (reading from the source stream, writing to the destination stream) in chunks. In the loop that is required to copy all the fragments, you can check whether the operation should be interrupted, and then perform the necessary operations to interrupt the copy process.
Source: https://habr.com/ru/post/1749933/More articles:FileVersionInfo.GetVersionInfo gets an old version of exe replaced at runtime - c #Override one css class with another? - jquerySearch HTML table with jQuery - javascriptКак удалить список из - phphttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1749932/can-i-tell-sitecrawlers-to-visit-a-certain-page&usg=ALkJrhgZyKFLZLWu2zDeWeiJscRk6-_eugHow to enter PHP code from a database into a PHP script? - databasehttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1749935/mapping-integers-to-types-using-c-template-fails-in-a-specific-case&usg=ALkJrhhxztsnCMylLmIIvMbtB9dt-8h9oQPython singleton pattern - pythonCapturing only the top record from a LINQ query - c #How to combine these two PHP arrays? - arraysAll Articles