How can I get the system time??!? I looked at System.DateTime, and I see how to represent an instant of the day, but I want the actual time...
Thx, Xmas
Printable View
How can I get the system time??!? I looked at System.DateTime, and I see how to represent an instant of the day, but I want the actual time...
Thx, Xmas
DateTime.Now.TimeofDay
Ok, but how I compare two times? Let's say I want to wait for 10 seconds...
That code doesn't work.VB Code:
Dim TimeNow As New DateTime() TimeNow = DateTime.Now While (DateTime.Now.Compare(TimeNow, DateTime.Now) < 10) 'Do nothing here... Just wait. End While
Pls help.
Thx
Xmas
If you just want to pause for 10 seconds then this is easier.
Threading.Thread.CurrentThread.Sleep(1000)
here is an example to compare two given times .
ooops forgot the zip file.sorry
here you go
.
.
.
.
;)
Edneeis:
I don't want to stop a thread for 10 secs, but I want to see if a connection goes in timeout. So the while cicle would be like that:pirate:VB Code:
while TimeElapsed<timeout and ConnectionNotEstablished end while
Thanks for you code, it'll get me start, but I was wandering if exists a predefined function in system.datetime... That "good" guys at Microsoft must have been thinking about a thing like that...
VB Code:
Dim TimeNow As New DateTime() TimeNow = Now While (Now.Subtract(TimeNow).Seconds < 10) 'Wait for timeout...Do nithing... End While
If you execute this on the same thread as the process you are waiting for then it probably wont be much better than sleep. You should at least have an Application.DoEvents in the loop. Better yet you can use a delegate or threading so that your application doesn't hang. If I'm wrong and it works fine then sorry :DQuote:
Originally posted by Xmas79
VB Code:
Dim TimeNow As New DateTime() TimeNow = Now While (Now.Subtract(TimeNow).Seconds < 10) 'Wait for timeout...Do nithing... End While
You're right, I was so happy to find how to "compare two times" that I posted the code without too warnings on it.:D I need an Application.DoEvents in message loop obviously, but only for avoiding my form from freezing (nice word joke isn't it?). In fact I have an asyncronous connection listener.
Thx and Goob job.
Xmas
I agree with Edneeis that best method is to use thread sleep.
The synstax what he said but the timeout scale is milliseconds so you have to use 10000 to wait 10 seconds.
And That piece of code:
While (Now.Subtract(TimeNow).Seconds < 10)
compares two integer values which are the seconds of the current time. Obviously if the time you begin to compare the second is 55 for example, then 10 seconds later this value is 5 and the substraction will not work for you as you desired. So if you are obssessed with using times you should use the smallest cycle of time which is 'ticks' each one equal to 100 nanoseconds
Something like this will help you:
Code:
Dim dt As Long
MsgBox("waiting")
dt = DateTime.Now.TimeOfDay.Ticks
While DateTime.Now.TimeOfDay.Ticks < dt + 100000000
'nothing to do here
End While
msgbox("Continued")
I think I wouldn't wait at all I'd just use a delegate and BeginInvoke. That way you just start the connection then go about your business and it'll let you know when its done, then you can continue where you left off.
VB Code:
Public Delegate Sub DoConnection(ByVal info As String) Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click Dim del As New DoConnection(AddressOf MakeConnection) 'invoke the method asynchronously so we don't have to wait del.BeginInvoke("Here is some connection info", New AsyncCallback(AddressOf ConnectionMade), Nothing) End Sub Private Sub ConnectionMade(ByVal ar As IAsyncResult) If ar.IsCompleted Then 'this is our callback to let us know the method is finished 'here we could use the connection MsgBox("connection made") End If End Sub Public Sub MakeConnection(ByVal info As String) 'here is the method to make the connection that takes a while 'we will simulate a long method with sleep Threading.Thread.CurrentThread.Sleep(10000) End Sub
I'm time obsessed??!? YOU are speaking about nanoseconds:D:D:D!!!! Yes, there is a little problem with subtraction which can be resolved changingwith the betterVB Code:
Now.Subtract(TimeNow).Seconds < 10There are also other properties:VB Code:
Now.Subtract(TimeNow).TotalSeconds < 10
.TotalDays
.TotalHours
.TotalMinutes
.TotalSeconds <---- the one I used in the above example:D
.TotalMilliseconds
They all convert the value of the time from ticks to their respective units. They have whole and fractional part. Like your code, but with less 00000000000000000000000000000000!!!!!!!
Edneeis: Why your code is better than mine? I can't understand why... Could you explain me the idea of your code? I think the idea of my code is quite clear....
Thx
Xmas.
Its not so much a matter of yours is better than mine, its just different ways of doing the same thing.
My code just lets the framework take care of threading the long call and then notifies when it is done, instead of pausing the application and looping. Once the BeginInvoke method is called you could continue to call other methods or the user can interact freely with the form whereas the loop will cause lag and you can no longer process anything else. Sometimes you don't want anything to happen until a method is finished though.
Its just different ways of doing the samething, if you like yours stick to it. Its all good.
Xmas
Totals were cool, thank you!