Wait for method to finish

2020-03-03 07:53发布

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

标签: c#
3条回答
Evening l夕情丶
2楼-- · 2020-03-03 08:24

I assume you are asking how to wait for a Code executing on another Thread in your main Thread . For that purpose Thread.Join() method will do what you want.

(A nice tutorial on how to do Multithreading.)

查看更多
再贱就再见
3楼-- · 2020-03-03 08:26

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)
查看更多
够拽才男人
4楼-- · 2020-03-03 08:27
  1. Call the method.
  2. Wait for it to finish.

Note: Only works for blocking calls.

查看更多
登录 后发表回答