Results 1 to 17 of 17

Thread: Now!!!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    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...

  2. #2

  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    DateTime.Now.TimeofDay

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    Ok, but how I compare two times? Let's say I want to wait for 10 seconds...
    VB Code:
    1. Dim TimeNow As New DateTime()
    2.  
    3. TimeNow = DateTime.Now
    4. While (DateTime.Now.Compare(TimeNow, DateTime.Now) < 10)
    5. 'Do nothing here... Just wait.
    6. End While
    That code doesn't work.
    Pls help.

    Thx
    Xmas
    Learn, this is the Keyword...

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you just want to pause for 10 seconds then this is easier.

    Threading.Thread.CurrentThread.Sleep(1000)

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    here is an example to compare two given times .

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    ooops forgot the zip file.sorry
    here you go
    .
    .
    .
    .
    Attached Files Attached Files

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    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:
    1. while TimeElapsed<timeout and ConnectionNotEstablished
    2. 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...

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    SOLVED

    VB Code:
    1. Dim TimeNow As New DateTime()
    2.  
    3. TimeNow = Now
    4. While (Now.Subtract(TimeNow).Seconds < 10)
    5. 'Wait for timeout...Do nithing...
    6. End While
    Learn, this is the Keyword...

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: SOLVED

    Originally posted by Xmas79
    VB Code:
    1. Dim TimeNow As New DateTime()
    2.  
    3. TimeNow = Now
    4. While (Now.Subtract(TimeNow).Seconds < 10)
    5. 'Wait for timeout...Do nithing...
    6. 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

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    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...

  12. #12
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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")

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Public Delegate Sub DoConnection(ByVal info As String)
    2.  
    3.     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    4.         Dim del As New DoConnection(AddressOf MakeConnection)
    5.         'invoke the method asynchronously so we don't have to wait
    6.         del.BeginInvoke("Here is some connection info", New AsyncCallback(AddressOf ConnectionMade), Nothing)
    7.     End Sub
    8.  
    9.     Private Sub ConnectionMade(ByVal ar As IAsyncResult)
    10.         If ar.IsCompleted Then
    11.             'this is our callback to let us know the method is finished
    12.             'here we could use the connection
    13.             MsgBox("connection made")
    14.         End If
    15.     End Sub
    16.  
    17.     Public Sub MakeConnection(ByVal info As String)
    18.         'here is the method to make the connection that takes a while
    19.         'we will simulate a long method with sleep
    20.         Threading.Thread.CurrentThread.Sleep(10000)
    21.     End Sub

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    I'm time obsessed??!? YOU are speaking about nanoseconds!!!! Yes, there is a little problem with subtraction which can be resolved changing
    VB Code:
    1. Now.Subtract(TimeNow).Seconds < 10
    with the better
    VB Code:
    1. 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...

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    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...

  16. #16
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  17. #17
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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
  •  



Click Here to Expand Forum to Full Width