[RESOLVED] How can I restart a thread at midnight?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: [RESOLVED] How can I restart a thread at midnight?

Hybrid View

  1. #1
    Join Date
    Jan 2012
    Location
    USA
    Posts
    89

    [RESOLVED] How can I restart a thread at midnight?

    [EDITED]

    Hello,

    I am running Ubuntu 11.04.

    I have an application that downloads data from the remote web server. This web server shuts down for maintenance at midnight for about ten minutes. So I need to disconnect from the server at 11:55pm and connect again at 12:05am. I use sockets so to disconnect I need to logout and close the socket, and to re-connect I need to open socket and logon. The part of my application that downloads data runs in its own thread.

    So I need to stop the thread at 11:55pm and start at 12:05am. How can I do that in non-blocking way? I do not want to run a loop and poll for system time that will suck up CPU time.

    TIA!
    Last edited by vincegata; January 19th, 2012 at 02:30 PM.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: How can I restart a thread at midnight?

    The typical approach in this situation has nothing to do with the actual time; it just detects when the server is no longer connected (typically using a timeout), and then keeps trying to reconnect until it succeeds.

    In order to avoid having these reconnection attempts use too many resources, it is common to double the amount of wait time after each failed connection attempt, up to some reasonable maximum (perhaps 2 minutes).

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,057

    Re: How can I restart a thread at midnight?

    If it were me, I'd set a timer to go off every minute or so and check the system time. Overhead would be minimal.

  4. #4
    Join Date
    Jan 2012
    Location
    USA
    Posts
    89

    Re: How can I restart a thread at midnight?

    Quote Originally Posted by GCDEF View Post
    If it were me, I'd set a timer to go off every minute
    How exactly do I do that? Run sleep(60) in separate thread?
    Last edited by vincegata; January 19th, 2012 at 01:42 PM.

  5. #5
    Join Date
    Jan 2012
    Location
    USA
    Posts
    89

    Re: How can I restart a thread at midnight?

    Quote Originally Posted by Lindley View Post
    The typical approach in this situation has nothing to do with the actual time; it just detects when the server is no longer connected (typically using a timeout), and then keeps trying to reconnect until it succeeds.
    I need this too to make my app robust - to reconnect if the server unexpectedly drops the connection. I do not know how to check if the server is no longer connected. I'll post this question in sockets sub-forum.

    Thank you.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: How can I restart a thread at midnight?

    If it's a TCP socket, it has internal means to validate that the connection is still active. If the connection goes down, you typically get an error from any function you call tell you so (send, recv, etc).

    If you're using UDP you will need to write the keepalive logic yourself.

  7. #7
    Join Date
    Jan 2012
    Location
    USA
    Posts
    89

    Re: How can I restart a thread at midnight?

    Quote Originally Posted by Lindley View Post
    If it's a TCP socket, it has internal means to validate that the connection is still active. If the connection goes down, you typically get an error from any function you call tell you so (send, recv, etc).
    I see, thank you. It's TCP.

  8. #8
    Join Date
    Jan 2012
    Location
    USA
    Posts
    89

    Re: How can I restart a thread at midnight?

    I am gonna use setitimer, or sleep(60) in separate thread. I'll see what's better/easier.

    Thank you for suggestions.

    SOLVED.

  9. #9
    Join Date
    Nov 2003
    Posts
    1,787

    Re: How can I restart a thread at midnight?

    >> I'll see what's better/easier.
    The better/easier approach was given in post #2. You don't need timers or sleeps.

    gg

  10. #10
    Join Date
    Jan 2012
    Location
    USA
    Posts
    89

    Re: How can I restart a thread at midnight?

    Quote Originally Posted by Codeplug View Post
    The better/easier approach was given in post #2. You don't need timers or sleeps.
    gg
    You are probably right, I need my app to be able to recover from connection loss anyway.

  11. #11
    Join Date
    Nov 2003
    Posts
    1,787

    Re: How can I restart a thread at midnight?

    >> I need my app to be able to recover from connection loss anyway.
    At least you won't have to change to clock to test your recover code - just pull the plug

    gg

  12. #12
    Join Date
    Jan 2012
    Location
    USA
    Posts
    89

    Re: How can I restart a thread at midnight?

    >> At least you won't have to change to clock to test your recover code - just pull the plug

    that's also true

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center