Calling function after a halt
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Calling function after a halt

  1. #1
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Calling function after a halt

    I want my application to run particular code after 1hr and so on. Its a checking some file program after a particular time period of 1hr. Then again check it after 1hr. The application starts with system startup.

    Im writing it in raw winapi and how do I call a function with a gap of 1hr. Im trying from window callback function but dont think that this is the proper way.

    Now Im just putting that function in WM_INITDIALOG and its just for 1 time running purpose. How do I make this function to call automatically after a halt (ie: using Sleep) of 1hr. ?

  2. #2
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Calling function after a halt

    If your main thread should remain responsible you need to set up a thread which does the job.

    Code:
       _beginthread(ThreadFunc, 0, 0);
    To wait for an hour in a thread call

    Code:
        void ThreadFunc(void* p)
        {
            Sleep(3600000);
            doWhatMustBeDone();
        }
    For a periodical thing do the above into a loop.

    Regards, Alex

  3. #3
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Calling function after a halt

    Thats not the problem, I can use multithread but where to put this _beginthread() for calling the function.

    I mean where do I place this new thread inside my application window procedure ie: callback function.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,205

    Re: Calling function after a halt

    Why not just use a while loop with a sleep in it, or a timer?

  5. #5
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Calling function after a halt

    It may also be worth looking at Windows Task Scheduler, e.g. http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,441

    Re: Calling function after a halt

    Quote Originally Posted by hypheni View Post
    Thats not the problem, I can use multithread but where to put this _beginthread() for calling the function.

    I mean where do I place this new thread inside my application window procedure ie: callback function.
    Have you thought to try some threading samples/tutorials from the internet? Wouldn't looking through some of these be a good place to start?


    __________________________________________________________
    Arjay

    See my latest series on using WCF to communicate between a Windows Service and WPF task bar application.
    Tray Notify - Part I Tray Notify - Part II

    Need a little help with Win32 thread synchronization? Check out the following CG articles and posts:
    Sharing a thread safe std::queue between threads w/progress bar updating
    Simple Thread: Part I Simple Thread: Part II
    Win32 Thread Synchronization, Part I: Overview Win32 Thread Synchronization, Part 2: Helper Classes

    www.iridyn.com


  7. #7
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Calling function after a halt

    Okay. Can I use SetTimer in InitDialog and then process the function in wm_timer. And I think the multithreading part should be used here for not hanging the application.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,441

    Re: Calling function after a halt

    Quote Originally Posted by hypheni View Post
    Okay. Can I use SetTimer in InitDialog and then process the function in wm_timer. And I think the multithreading part should be used here for not hanging the application.
    Right. So maybe a good approach is to start by looking at other threading examples from the internet.


    __________________________________________________________
    Arjay

    See my latest series on using WCF to communicate between a Windows Service and WPF task bar application.
    Tray Notify - Part I Tray Notify - Part II

    Need a little help with Win32 thread synchronization? Check out the following CG articles and posts:
    Sharing a thread safe std::queue between threads w/progress bar updating
    Simple Thread: Part I Simple Thread: Part II
    Win32 Thread Synchronization, Part I: Overview Win32 Thread Synchronization, Part 2: Helper Classes

    www.iridyn.com


  9. #9
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Calling function after a halt

    I cleared all my doubts from my previous post related about _beginthread.

    But tell me one thing is WM_INITDIALOG the right place to put this code ?. Cause my application will run until unless user clicks on it.

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,441

    Re: Calling function after a halt

    Instead of using _beginthread, you'll want to use _beginthreadex.

    Also, don't use Sleep( ) with a long timeout because with sleep you lose control over the thread.

    Instead use WaitForSingleObject( ) inside the thread with a timeout.

    Something like:
    Code:
    switch( ::WaitForSingleObject( ..., LONG_TIMEOUT_VALUE ) )
    {
    case WFSO_SHUTDOWN_INDEX:
      return 1;  // shutdown event set, so bail
    case WAIT_TIMEOUT:
      DoWorkOnTimeout( );
      break;
    }
    By the sound of what you are trying to do, it sounds like you need a service app more than a Windows app.

  11. #11
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Calling function after a halt

    Its an application which fetches some data from internet and display it on a dialogbox.

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,441

    Re: Calling function after a halt

    Okay. Keep in mind that you won't be able to directly update the UI from the worker 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center