How to save time on multiple file zipping
Hiya. I have a tool that builds and copies files into specified directory and then zips them. It is currently consists of the following:
Class {
Foreach () {
F1 (); // copying files
Zip(); // zipping these files
F2(); F3();
Zip(); // zipping F1, F2 & F3 into different archive
}}
I would like to call Zips and while they are zipping, perform further until it hits another Zip function and start when previous zipping is finished. I’ve tried to play around with async (currently learning), but not exactly sure how to work it out. Could anyone advice please?
12 Replies
async is the answer.
from your outline, its a bit unclear what the foreach above is?
the core idea of async is that you get an object called a "Task" back that represents the execution of your method
you can check the status on it and most importantly, await it. Awaiting a task means "wait until that task finishes"
so for example, you could start zipping three different things and when they are all done, zip those 3 zips
Thank you for your response @Pobiega! Foreach is loops through folders to grab stuff from
It does sound like I'd need to restructure the zipping
So, what is the stuff you would like to do asynchronously? The zipping of the folders?
Note that there isn't an immediately-obvious API for asynchronous file copying (due to limitations in the Win32 API iirc). I think you have to go through the
FileStream
class
Yes, so the tool wouldn't wait for stuff to zip and just leave it to perform on the side.
sure
just start your zipping, put their tasks in a list, then at the end before terminating you just await all the tasks in the list
obviously at some point you need to let them finish, or you get weird behaviour
Might do, but I was thinking to leave Zip as only async one and perform other copyings and zipping at the same time
it will depend on whether your Zip method is async
if it doesn't offer an awaitable method, you'll have to
Task.Run
it which won't be ideal@Pobiega that makes sense. Will it work in the for/foreach loop?
can you show
Zip()
?
this is very much implementation dependant
Zip is properly async here, then yeah this will work, except that it will do one iteration of the loop at a time
if you need it to start all the tasks at once, you'll need to have a list outside the loop you add the tasks to, and await after the loopI've replicated as implementation is on other laptop, sorry 😅
.net has built in zip support