无法取消Zip文件创建:使用ZipFile.CreateFromDirectory ?? [英] Can't cancel Zip file creation: Using ZipFile.CreateFromDirectory ??
问题描述
tokenSourceZip = new CancellationTokenSource();
var token = tokenSourceZip.Token;
try
{
await Task.Run(()=>
{
try
{
string path = $" {zipFolder.Path} \\ {srcFolder.Name} _ {Guid.NewGuid()}。zip";
ZipFile.CreateFromDirectory(srcFolder.Path,path,
CompressionLevel.Fastest ,true);
System.Diagnostics.Debug.WriteLine(" folder zipped");
}
catch(例外e2)
{
System.Diagnostics.Debug.WriteLine(e2.Message);
}
},令牌);
}
catch(AggregateException ex)
{
System.Diagnostics.Debug.WriteLine(" Task aborted");
System.Diagnostics.Debug.WriteLine(" Exception messages:");
foreach(var在ex.InnerExceptions中)
System.Diagnostics.Debug.WriteLine(" {0}:{1}",ie.GetType()。Name,ie.Message);
}
最后
{
//tokenSourceFolders.Dispose();
}
尝试在使用
ZipFile.CreateFromDirectory。
我已将其设置为上述等待任务。
但是当我通过令牌取消时,任务继续。
鉴于要压缩的文件夹很深(实际上在删除之前备份)我需要能够异步取消操作。
取消行动是:
if(tokenSourceZip!= null)
{
if( !tokenSourceZip.IsCancellationRequested)
if(tokenSourceZip.Token!= null)
if(tokenSourceZip.Token.CanBeCanceled)
tokenSourceZip.Cancel();
}
任何想法?
Thx
嵌入式MVP
嗨大卫琼斯
如果这个同步
方法正在运行,似乎无法取消Zipfile.CreateFromDirectory进程,
但我有一个解决方法在这里,您可以尝试延迟ZipFile.CreateFromDirectory
方法一段时间,在此期间,取消工作。
您可以参考以下代码:
await Task.Delay(10000);
tokenSourceZip.Token.ThrowIfCancellationRequested();
ZipFile.CreateFromDirectory(zipFolder.Path,path,
CompressionLevel.Fastest,true);
您可以根据需要设置延迟时间。因此,如果您需要取消任务,您可以有时间决定
。
< p style ="margin-bottom:0.0001pt; line-height:normal">
最好的问候,
罗伊跨度>
tokenSourceZip = new CancellationTokenSource(); var token = tokenSourceZip.Token; try { await Task.Run(() => { try { string path = $"{zipFolder.Path}\\{srcFolder.Name}_{Guid.NewGuid()}.zip"; ZipFile.CreateFromDirectory(srcFolder.Path, path, CompressionLevel.Fastest, true); System.Diagnostics.Debug.WriteLine("folder zipped"); } catch (Exception e2) { System.Diagnostics.Debug.WriteLine(e2.Message); } }, token); } catch (AggregateException ex) { System.Diagnostics.Debug.WriteLine("Task aborted"); System.Diagnostics.Debug.WriteLine("Exception messages:"); foreach (var ie in ex.InnerExceptions) System.Diagnostics.Debug.WriteLine(" {0}: {1}", ie.GetType().Name, ie.Message); } finally { //tokenSourceFolders.Dispose(); }
Trying to enable cancellation of a Zip file creation when using
ZipFile.CreateFromDirectory.
I have set it to run as an awaited task as above.
But when I cancel via the token the task continues.
Given that the folders to be zipped are deep (actually backing up before some deletions) I need to be able to asynchronously cancel the action.
Cancellation is action by:
if (tokenSourceZip != null) { if (!tokenSourceZip.IsCancellationRequested) if (tokenSourceZip.Token != null) if (tokenSourceZip.Token.CanBeCanceled) tokenSourceZip.Cancel(); }
Any ideas?
Thx
Embedded MVP
Hi David Jones
It seems not possible to cancel the Zipfile.CreateFromDirectory process if this synchronous method is running,
But I have a workaround here, you could try to delay the ZipFile.CreateFromDirectory method for some time, and during this time, the cancellation works.
You could refer the following code:
await Task.Delay(10000); tokenSourceZip.Token.ThrowIfCancellationRequested(); ZipFile.CreateFromDirectory(zipFolder.Path, path, CompressionLevel.Fastest, true);
You could set the delay time as you want. So you could have a time for you to decide if you need to cancel the task.
Best regards,
Roy
这篇关于无法取消Zip文件创建:使用ZipFile.CreateFromDirectory ??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!