c# how do you get code to wait for function to compleate before moving onto another function.

mion shion 241 Reputation points
2022-11-22T17:01:59.373+00:00

Hello all i know what i have just asked seems basic,

however, i have 5 functions 2 of which download a file and install it and i trying to find a way to stop the application from going onto the next one before the process finishes is there a way to do this

so i want it to load the download file and install once the installation has been completed move on to the next function

i have tyied the task and await but still moving on,

functions,

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

and now it seems to do vs install then check python all together then run the install at the end and it wont work if the above programs have not been installed first and for the life of me cant figure out how i would get it wait for the install then move onto the next function in question

is there any way this can be a thing.

kind regards,
elfenliedtopfan5

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,161 Reputation points
    2022-11-22T17:43:42.67+00:00

    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()  
       }  
    
    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-11-22T17:57:01.617+00:00

    You don’t show the function’s implementation so we can only guess. If they are not tasks, then typically they use a callback or mutex. You wrap this code with a task wrapper.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.