Ryan Currah
Ryan Currah

Reputation: 1105

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 :)

Upvotes: 4

Views: 13900

Answers (2)

Stephen Cleary
Stephen Cleary

Reputation: 456577

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.

Upvotes: 14

zs2020
zs2020

Reputation: 54524

Try:

Task.Factory.StartNew(() =>
{
    ZipFile.ExtractToDirectory(tempPath + @"\" + ftpFile, pvtdest)
}

You should include System.Threading.Tasks;

Upvotes: 1

Related Questions