如何在vb.net中实现小的延迟?

发布于 2024-11-05 14:49:49 字数 701 浏览 0 评论 0原文

我想创建一个小的延迟,以便我的第一组代码顺利运行。

我怎样才能在 vb.net 中做到这一点?

编辑1

假设我有几行这样的代码

..................Statement Line 1..............
..................Statement Line 2..............
..................Statement Line 3..............
..................Statement Line 1..............
..................Statement Line 5..............

WAIT UNTIL STATEMENT 5 IS COMPLETED

..................Statement Line 6..............
..................Statement Line 7..............
..................Statement Line 8..............
..................Statement Line 9..............
..................Statement Line 10.............

只有当前五个语句执行完成时才可以执行接下来的五个语句

I want to create a small delay so that my 1st set of codes run smoothly.

How can I do that in vb.net ?

Edit 1

Suppose I have a few lines of code like this

..................Statement Line 1..............
..................Statement Line 2..............
..................Statement Line 3..............
..................Statement Line 1..............
..................Statement Line 5..............

WAIT UNTIL STATEMENT 5 IS COMPLETED

..................Statement Line 6..............
..................Statement Line 7..............
..................Statement Line 8..............
..................Statement Line 9..............
..................Statement Line 10.............

Only when the execution of first five statements are complete then only the next five can be executed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

﹎☆浅夏丿初晴 2024-11-12 14:49:49

您想让线程休眠吗?

线程睡眠(100);

其中 100 是您希望线程休眠的毫秒数。

还要确保有 Imports System.Threading,如果您已经有多个线程,我假设您已经拥有了。

编辑:好的,你已经添加了一些代码。不过,这应该取决于您是否有多个线程正在运行,并且从您的问题来看,它似乎全部都在一个线程中。在这种情况下,语句 5 始终会在语句 6 运行之前先完成。这就是代码的工作原理。唯一不会出现这种情况的情况是语句 1-5 之一在新线程上生成某些内容。

Are you looking to make a thread sleep?

Thread.Sleep(100);

Where 100 is the number of millisecond you want the thread to sleep for.

Also make sure to have Imports System.Threading, which I assume you have if you already have multiple threads.

EDIT: Okay, so you've added a bit of code. Still, this should come down to whether you have more than one thread running, and from your question it looks like it's all in one thread. In this case, statement 5 will always finish first before statement 6 runs. That's how code works. The only case where it wouldn't would be if one of statements 1-5 spawns something on a new thread.

梦萦几度 2024-11-12 14:49:49

首先 - 鉴于您提供的代码示例,第 6 行在第 5 行完成之前不会执行。你不需要做任何事;除非第 5 行是踢外部应用程序或创建新线程。

除此之外 -

Thread.Sleep 会引入延迟,但通常情况下它并不是您想要的。

如果您使用 Thread.Sleep,执行线程将休眠您指定的时间。但是您的示例代码表明您希望线程等待直到满足某些条件。假设您正在等待在您正在休眠的线程之外发生的条件,那么最多您最终会得到一个循环,该循环会保持休眠 X 毫秒,然后检查条件。

还有其他方法比这更容易(从长远来看)/更强大。如果您确实希望在另一个线程上发生某些事情并在其完成时收到警报;考虑BackgroundWorker 类。

http://msdn.microsoft.com/en-us/library /system.componentmodel.backgroundworker.aspx

对于简单的多线程任务来说非常方便。您创建一个BackgroundWorker,并使用新线程上发生的逻辑来处理它的“DoWork”事件,并处理“Completed”事件(检查文档以获取正确的名称,我是从内存中获取的)。您调用“RunWorkerAsynch”来启动该进程,当“Worker_Completed”事件触发时,您可以继续执行第 6 行。

我希望这有意义/有帮助。

First - given the code sample you've provided line 6 will not execute until line 5 finishes. You don't need to do anything; unless line 5 is kicking of an external application or creating a new thread.

Beyond that -

Thread.Sleep will introduce a delay, but more often than not it really isn't what you are looking for.

If you use Thread.Sleep the executing thread will sleep for however long you tell it. But your sample code indicates you want the thread to wait UNTIL some condition is met. Assuming you are waiting on a condition that happens outside of the thread you are sleeping, at best, you'd end up with a loop that keeps sleeping for X milliseconds and then checking the condition.

There are other approaches that are easier (in the long run)/more robust than that. If you truly want something to happen on another thread and to be alerted by it's completion; consider the BackgroundWorker class.

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

It's very handy for simple multithreading tasks. You create a BackgroundWorker and handle it's 'DoWork' event with the logic to happen on the new thread and handle the 'Completed' event (check the docs for the correct name, I'm going from memory). You call 'RunWorkerAsynch' to start the process and when the 'Worker_Completed' event fires, you can continue execution of line 6.

I hope that makes sense/helps.

哆啦不做梦 2024-11-12 14:49:49

我认为

Application.DoEvents()

应该这样做。

I think

Application.DoEvents()

should do that.

倦话 2024-11-12 14:49:49

使用BackgroundWorker 对象属性“IsBusy”,并且在工作进程繁忙之前不允许执行第6 行。

此处

Use BackgroundWorker object property "IsBusy" and do not allow to execute line 6 until the worker process is busy.

Read more about BackgroundWorker here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文