开发者

VB.NET equivalent of Timeout.bas Module from VB6

开发者 https://www.devze.com 2023-02-01 23:15 出处:网络
I am looking for the VB.NET code of a very handy little *.bas file I used to use in Visual Basic 6.The file was called timeout.bas and it was the greatest module ever to me.I want to switch to start u

I am looking for the VB.NET code of a very handy little *.bas file I used to use in Visual Basic 6. The file was called timeout.bas and it was the greatest module ever to me. I want to switch to start using VB.NET finally but this single file is holding me back. Trying to use .NET without it is like crippling me.开发者_如何学编程 Can someone, anyone please make this code work in .NET for me?

It's only a couple lines:

Attribute VB_Name = "Module1"
Sub timeout(duration)
starttime = Timer
Do While Timer - starttime < duration
DoEvents
Loop
End Sub

Basically you add that timeout.bas file which contains that code and you can just do:

Text1.text = "hello"
timeout .5
Text1.text "World!"

It's so awesome. Anyone PLEASE re-do it in VB.NET for me! Thanks!


That's the worst attempt at non-blocking busy loop.

You can do the literal translation to VB.Net by putting in the while loop the following two lines:

System.Threading.Thread.Current.Sleep(1000)
Application.DoEvents

This would sleep for a second and then process the message loop on each iteration.

However, you should really ask yourself why do you need to wait for certain period of time? What are you waiting for and can't you wait for it in different way? If the main thread is waiting for a background worker to finish, you can just let the main thread continue and process the message loop as normal, and the background worker can just notify it using Dispatcher to send the message.


Sub timeout(ByVal duration As Double)
    Dim starttime = DateTime.Now
    Do While (DateTime.Now  - starttime).TotalSeconds < duration
        My.Application.DoEvents()
    Loop
End Sub

Set the form's Enable property to false when you use this.


You could do:

System.Threading.Thread.Sleep(5000);

Thant sort of thing is discouraged in .Net and will lock up your UI.

try this

Sub timeout(seconds)
   Dim endtime = DateTime.Now.AddSeconds(seconds)
   Do While DateTime.Now < endtime
     threading.thread.sleep(100)
     application.doevents
   Loop
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消