Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract a zip file Asynchronously in c# to not block the UI?

There doesn't seem to be a method in C# where you can unzip a file using await, so I created a task and I am trying to await that. I get the following error:

Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task'

When I run this code..

Task taskA = await Task.Run(() => ZipFile.ExtractToDirectory(tempPath + @"\" + ftpFile, pvtdest));

Any thoughts on this would be greatly appreciated! Thank you :)

like image 443
Ryan Currah Avatar asked Dec 12 '22 13:12

Ryan Currah


1 Answers

Just remove the Task taskA =, like this:

await Task.Run(() => ZipFile.ExtractToDirectory(tempPath + @"\" + ftpFile, pvtdest));

Once you await a Task, you usually don't need to do anything else. A Task doesn't have a result value, so that's why the compiler is complaining about "void". The await will handle propagating exceptions and continuing the method when your Task.Run is complete, and that should be all you need.

like image 120
Stephen Cleary Avatar answered May 02 '23 10:05

Stephen Cleary