-
August 12th, 2010, 04:47 AM
#1
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. ?
-
August 12th, 2010, 04:57 AM
#2
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
-
August 12th, 2010, 05:00 AM
#3
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.
-
August 12th, 2010, 07:26 AM
#4
Re: Calling function after a halt
Why not just use a while loop with a sleep in it, or a timer?
-
August 12th, 2010, 07:47 AM
#5
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
-
August 12th, 2010, 08:19 AM
#6
Re: Calling function after a halt
Originally Posted by hypheni
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
-
August 12th, 2010, 08:57 AM
#7
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.
-
August 12th, 2010, 08:59 AM
#8
Re: Calling function after a halt
Originally Posted by hypheni
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
-
August 12th, 2010, 09:04 AM
#9
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.
-
August 12th, 2010, 10:47 AM
#10
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.
-
August 12th, 2010, 10:52 AM
#11
Re: Calling function after a halt
Its an application which fetches some data from internet and display it on a dialogbox.
-
August 12th, 2010, 12:03 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|