In C#, by default methods are synchronous so when you call a function it won't return until the function all completes.
ffmpegsetup(ffmpegpathset);
// lets now start waiting for the downloads to complete before doing anything
vsinstall();
checkpythonver();
checkpython();
// only run this when the otheres have finished downloading and installing.
installspotdl()
Assuming none of these functions return Task
then each one will be called and block until the call completes. So they run synchronous already.
Where things get more interesting is if you want to run multiple functions at once and wait for them all to complete. In that case your dealing with async. But your functions have to be written to support this. Suppose, in the example above, you want to run the middle set of functions all at once and only when they are all done move on. In that case they need to be async.
Task vsinstall ()
{
//Do async work here...
}
Task checkpythonver ()
{
//Do async work here...
}
Task checkpython ()
{
//Do async work here...
}
If you want to wait for the async work to complete before you move on then that is what the await
keyword is for. Note that you can only use it in a method that is itself async.
async Task DoWork ()
{
//Sync call, block until done
ffmpegsetup(ffmpegpathset);
//They are async so wait for each one to complete before moving to the next one
await vsinstall();
await checkpythonver();
await checkpython();
//All the previous functions are done so continue on
installspotdl()
}
await
is used on functions that return Task
when you want to wait for them to complete (like a normal method). If you want to run several async tasks at once and then wait until they are all complete then that is slightly different code.
Task DoWork ()
{
//Sync call, block until done
ffmpegsetup(ffmpegpathset);
//Start the async work but don't wait for them to finish
var task1 = vsinstall();
var task2 = checkpythonver();
var task3 = checkpython();
//Async work started, now wait for them to complete
Task.WaitAll(new [] { task1, task2, task3 });
//All the previous functions are done so continue on
installspotdl()
}
If you need a mix of waiting and not then ideally move all this into a standalone async method and do the blocking there.
Task InstallStuffAsync ()
{
//Start the async work but don't wait for them to finish
var task1 = vsinstall();
var task2 = checkpythonver();
var task3 = checkpython();
//Async work started, now wait for them to complete
return Task.WhenAll(new [] { task1, task2, task3 });
}
async Task DoWork ()
{
//Sync call, block until done
ffmpegsetup(ffmpegpathset);
//Do install and wait until it is done
await InstallStuffAsync();
//All the previous functions are done so continue on
installspotdl()
}