Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for method to finish

Tags:

c#

How can I wait for a method to finish using C#?

like image 781
Pillblast Avatar asked Mar 14 '11 09:03

Pillblast


People also ask

How do you wait for a setTimeout to finish?

Use of setTimeout() function: In order to wait for a promise to finish before returning the variable, the function can be set with setTimeout(), so that the function waits for a few milliseconds. Use of async or await() function: This method can be used if the exact time required in setTimeout() cannot be specified.

How do you wait then to finish JavaScript?

To handle the asynchronous nature of JavaScript executions, you can use one of the three available methods to wait for a function to finish: Using callback functions. Using Promise object. Using async/await keywords.

How do you wait for a function to finish in node JS?

How to wait for async calls to finish Javascript. let first = new Promise((resolve, reject) => { // download first file resolve() }) let second = new Promise((resolve, reject) => { // download second file resolve() }) await Promise. all([first, second]) alert('Finished all downloads!


2 Answers

Unless you're using multiple threads, execution won't continue in the calling code until the method has completed anyway.

If you are using multiple threads, it really depends on how you're launching the task. For example, you could be using asynchronous delegate execution (foo.BeginInvoke(...)) or the Task Parallel Library, or simply creating a new thread. Each approach has its own way of waiting until the task/thread has completed. Please give us more information and we can help you more, but options may include:

  • Calling EndInvoke on the delegate, passing in the IAsyncResult returned by BeginInvoke
  • Calling Task.Wait (optionally with a timeout)
  • Calling Thread.Join (optionally with a timeout)
like image 110
Jon Skeet Avatar answered Sep 29 '22 10:09

Jon Skeet


  1. Call the method.
  2. Wait for it to finish.

Note: Only works for blocking calls.

like image 39
mdm Avatar answered Sep 29 '22 12:09

mdm