|
-
Dec 27th, 2002, 10:31 AM
#1
Thread Starter
Hyperactive Member
Now!!!
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
Learn, this is the Keyword...
-
Dec 27th, 2002, 10:36 AM
#2
PowerPoster
-
Dec 27th, 2002, 10:53 AM
#3
Frenzied Member
-
Dec 27th, 2002, 11:41 AM
#4
Thread Starter
Hyperactive Member
Ok, but how I compare two times? Let's say I want to wait for 10 seconds...
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
That code doesn't work.
Pls help.
Thx
Xmas
Learn, this is the Keyword...
-
Dec 27th, 2002, 11:52 AM
#5
If you just want to pause for 10 seconds then this is easier.
Threading.Thread.CurrentThread.Sleep(1000)
-
Dec 27th, 2002, 11:56 AM
#6
Sleep mode
here is an example to compare two given times .
-
Dec 27th, 2002, 11:58 AM
#7
-
Dec 27th, 2002, 12:39 PM
#8
Thread Starter
Hyperactive Member
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:
VB Code:
while TimeElapsed<timeout and ConnectionNotEstablished
end while
pirate:
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...
Learn, this is the Keyword...
-
Dec 27th, 2002, 12:53 PM
#9
Thread Starter
Hyperactive Member
SOLVED
VB Code:
Dim TimeNow As New DateTime()
TimeNow = Now
While (Now.Subtract(TimeNow).Seconds < 10)
'Wait for timeout...Do nithing...
End While
Learn, this is the Keyword...
-
Dec 27th, 2002, 12:58 PM
#10
Re: SOLVED
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
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
-
Dec 27th, 2002, 01:07 PM
#11
Thread Starter
Hyperactive Member
Right...
You're right, I was so happy to find how to "compare two times" that I posted the code without too warnings on it. 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
Learn, this is the Keyword...
-
Dec 27th, 2002, 01:18 PM
#12
Frenzied Member
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")
-
Dec 27th, 2002, 01:52 PM
#13
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
-
Dec 27th, 2002, 01:55 PM
#14
Thread Starter
Hyperactive Member
I'm time obsessed??!? YOU are speaking about nanoseconds  !!!! Yes, there is a little problem with subtraction which can be resolved changing
VB Code:
Now.Subtract(TimeNow).Seconds < 10
with the better
VB Code:
Now.Subtract(TimeNow).TotalSeconds < 10
There are also other properties:
.TotalDays
.TotalHours
.TotalMinutes
.TotalSeconds <---- the one I used in the above example
.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!!!!!!!
Learn, this is the Keyword...
-
Dec 27th, 2002, 02:02 PM
#15
Thread Starter
Hyperactive Member
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.
Learn, this is the Keyword...
-
Dec 27th, 2002, 02:46 PM
#16
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.
-
Dec 27th, 2002, 02:51 PM
#17
Frenzied Member
Xmas
Totals were cool, thank you!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|