等待方法完成

编程入门 行业动态 更新时间:2024-10-06 20:30:42
本文介绍了等待方法完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何等待使用C#的方法完成?

解决方案

除非您使用多个线程,否则在方法完全完成之前,不会在调用代码中继续执行.如果您正在使用多个线程,则实际上取决于您如何启动任务.例如,您可能正在使用异步委托执行( foo.BeginInvoke(...))或任务并行库,或者只是创建一个新线程.每种方法都有其自己的等待方式,直到任务/线程完成为止.请给我们更多信息,我们可以为您提供更多帮助,但是选项可能包括:

  • 在委托上调用 EndInvoke ,传入 BeginInvoke
  • 返回的 IAsyncResult
  • 调用 Task.Wait (可选,有超时)
  • 调用 Thread.Join (可以选择超时)

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

解决方案

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)

更多推荐

等待方法完成

本文发布于:2023-11-13 02:51:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583227.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!