VB .net 无法在非常短的时间内 sleep()

发布于 2024-12-05 15:57:23 字数 366 浏览 1 评论 0原文

我想在 VB 中为网络应用程序睡眠packetSize/Bandwidth持续时间。

该值的变化范围为“10^-3 - 10^-6”秒。

当我使用 Sleep() 函数时,如果它小于单位(< 1 秒 ~~ 0 秒??),我认为它不会在持续时间内休眠。

编辑:

  1. 在上述范围内短暂休息后,我必须继续发送数据包。因此,我无法指定,例如,在客户端休眠 0.001 毫秒。
  2. 我的要求是 CS 应用程序的客户端,它读取文件并以定期或不定期的睡眠时间间隔不断向服务器发送小数据包。随后,服务器捕获数据包并以自己的速率对其进行处理。

如何实现这一目标?

I would like to sleep for packetSize/Bandwidth amount of duration for a network application in VB.

This value varies from "10^-3 - 10^-6" seconds.

When I use the Sleep() func, I think it is not sleeping for the duration if it is less than unity (< 1 sec ~~ 0 sec??).

Edited:

  1. I have to keep sending packets after a short nap of above range. So, I could not specify, for eg., to sleep for 0.001 milliseconds at the client side.
  2. My requirement is for the client side of CS app, which reads a file and keeps sending small packets at regular or irregular interval of sleeps to the Server. The Server, later, captures the packets and processes it at its own rate.

How to achieve this?

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

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

发布评论

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

评论(4

相对绾红妆 2024-12-12 15:57:23

Sleep 的分辨率是毫秒 - 即 10^-3。 (请参阅http://msdn.microsoft.com/en-us/library/d00bd51t .aspxhttp://msdn.microsoft.com/en-us/library/274eh01d.aspx)。一秒是1000毫秒;那是睡眠(1000)。

对于小于 10^-3 秒的值,不能使用 Sleep()。我已成功使用值为 62、125 和 250 毫秒的 Sleep()。


您可以尝试使用 System.Diagnostics.Stopwatch;也许可以从中构建等待或暂停功能。 (请参阅http://msdn.microsoft.com/en-us/library/ebf7z0sw .aspx )

The resolution of Sleep is to the millisecond - that's 10^-3. (See http://msdn.microsoft.com/en-us/library/d00bd51t.aspx and http://msdn.microsoft.com/en-us/library/274eh01d.aspx ). One second is 1000 milliseconds; that's Sleep(1000).

You cannot use Sleep() for values less than 10^-3 seconds. I have used Sleep() with values of 62, 125, and 250 milliseconds successfully.


You can experiment with System.Diagnostics.Stopwatch; maybe a Wait or Pause function can be built from that. (See http://msdn.microsoft.com/en-us/library/ebf7z0sw.aspx )

Hello爱情风 2024-12-12 15:57:23

没有任何系统等待函数会以微秒分辨率超时。您需要一个繁忙的循环和一个高分辨率计时器。当然,系统可能会抢占您的线程,因此您可以忘记 Windows 上的实时保证。

There aren't any system wait functions that timeout at micro-second resolution. You would need a busy loop and a high resolution timer. Of course, the system may preempt your thread anyway so you can forget about realtime guarantees on Windows.

懵少女 2024-12-12 15:57:23

我怀疑你对这个问题想得太多了。网络通信有一个缓冲区,因为它们实际上无法期望应用程序准备好并等待传输的每一位。我建议阅读尽可能多的数据,短暂睡眠并重复。

Do Until all data is read
    While buffer contains data
        read data
    Wend
    ' Let other threads have a go, but come back as soon as possible.
    Thread.Sleep(0)
Loop

这符合您的目的吗?

I suspect you're overthinking the problem. Network commuications have a buffer because they can't realistically expect the application to be ready and waiting for every bit that is transmitted. I'd suggest reading as much data as is available, sleep briefly and repeat.

Do Until all data is read
    While buffer contains data
        read data
    Wend
    ' Let other threads have a go, but come back as soon as possible.
    Thread.Sleep(0)
Loop

Does this suit your purpose?

请止步禁区 2024-12-12 15:57:23

我正在学习 VB 中的 StopWatch 概念...以下代码块可以工作吗?

Private Sub MyWaiter(ByVal delay As Double)

    Dim sw As Stopwatch = New Stopwatch()

    sw.Start()

    Do While sw.ElapsedTicks < delay * TimeSpan.TicksPerSecond
        ' Allows UI to remain responsive
        Application.DoEvents()
    Loop

    sw.Stop()

End Sub

I am just studying about the StopWatch concept in VB... Will the following code block work?

Private Sub MyWaiter(ByVal delay As Double)

    Dim sw As Stopwatch = New Stopwatch()

    sw.Start()

    Do While sw.ElapsedTicks < delay * TimeSpan.TicksPerSecond
        ' Allows UI to remain responsive
        Application.DoEvents()
    Loop

    sw.Stop()

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