News | Articles | Libraries | Developer Tools | Books | Forum Links | Search   
Sections:
 

QA: How can I register my program to run at a certain time?

By Evan Golub, February 20, 2002.
http://www.cs.umd.edu/~egolub/PocketPC
Print version

Question

I would like to set it so that my program launches itself at some later time (even if my PocketPC is turned off). Can I do this and how?

Answer

Yes, you can register an application to be run at some later time. Yes, if your PocketPC is turned off at that time, it will turn on to run your application.

The API call you want is CeRunAppAtTime. This function will take the name of an application and the time you want to run that application.

eMbedded Visual Basic

First, you will need to add the following to the top of your code to be able to call the function:

Public Declare Function CeRunAppAtTime Lib "coredll" _ (ByVal AppName As String, ByVal ExecTime As String) As Boolean

Next, you need to build a String that contains a SYSTEMTIME. Since embedded Visual Basic does not have user-defined types, you'll need to assemble it "by hand". Fortunately, someone else has already created some very useful functions to do this. The following two functions are from and article by Antonio Paneiro:

Public Function IntegerToBinaryString(ByVal IntIn As Integer) As String Dim aux As Integer aux = IntIn: If IntIn < 0 Then aux = IntIn - &H8000 IntegerToBinaryString = IntegerToBinaryString & ChrB(aux Mod 256) aux = aux \ 256 If IntIn < 0 Then IntegerToBinaryString = IntegerToBinaryString & ChrB(aux + &H80) Else IntegerToBinaryString = IntegerToBinaryString & ChrB(aux) End If End Function

Public Function SYSTEMTIME( _ wYear As Integer, _ wMonth As Integer, _ wDayOfWeek As Integer, _ wDay As Integer, _ wHour As Integer, _ wMinute As Integer, _ wSecond As Integer, _ wMilliseconds As Integer) As String SYSTEMTIME = _ IntegerToBinaryString(wYear) & _ IntegerToBinaryString(wMonth) & _ IntegerToBinaryString(wDayOfWeek) & _ IntegerToBinaryString(wDay) & _ IntegerToBinaryString(wHour) & _ IntegerToBinaryString(wMinute) & _ IntegerToBinaryString(wSecond) & _ IntegerToBinaryString(wMilliseconds) End Function

You can now call SYSTEMTIME with the individual values (eg: year, month, etc.) and get back a String that is formatted correctly for use by the CeRunAppAtTime function. For example, say you wanted to allow a user of your program to press a button and have your program start again tomorrow at the same time of day that it currentlt is. You could add the following to a button's Click method:

Dim WakeUpTime As String Dim NewDate as Date 'take the current time and date and adds one day to it NewDate = DateAdd("d", 1, Now) 'create SYSTEMTIME structure in a String WakeUpTime = SYSTEMTIME(Year(NewDate), _ Month(NewDate), 0, _ Day(NewDate), _ Hour(NewDate), _ Minute(NewDate), _ Second(NewDate), 0) 'look up the path to the appliacation, the name of the application, 'tack on a ".vb" since this is an embedded Visual Basic app and 'call CeRunAppAtTime to set the event Call CeRunAppAtTime(App.Path + "\" + App.EXEName + ".vb", WakeUpTime)

The above code will build a SYSTEMTIME formatted date that is the current time of day, but tomorrow rather than today and schedule your application to be run at that time. Note that by using the DateAdd function, you do not have to concern yourself with sepcial cases such as the last day of the month - the function deals with those for you.

If the program that you have scheduled to execute is already running, it will be brought to the foreground if it is not already there.

eMbedded Visual C++

CeRunAppAtTime function is defined in the Notify.h header. Use GetModuleFileName function to get full path of the current application. You code can be like:

TCHAR lpFileName[MAX_PATH+1]; GetModuleFileName(NULL, lpFileName, MAX_PATH); SYSTEMTIME t; memset(&t, 0, sizeof(SYSTEMTIME)); t.wMonth = 5; t.wDay = 5; t.wYear = 2002; t.wHour = 11; CeRunAppAtTime(lpFileName, &t);

When it is necessary the system will start your application and will pass the APP_RUN_AT_TIME string as the command line. If your application is MFC based and one copy is already running then your running copy will be just brought to the foreground. But because the command line is not passed from the started copy to the running copy (MFC does not implement it) you will not be able to understand if the event happend.

To solve this problem you can make a "launcher" application that will start your main application if it is not running yet and will send some custom window message. Read more at:
How can I handle command line parameters in MFC based application?

Change/Remove

If you wish to change the time at which an application should be run, simply call CeRunAppAtTime again with the executable's path and name and the new execution time. This will delete the existing entry and create a new one with this new time. If you wish to cancel a scheduled event, you can call CeRunAppAtTime with the executable path and name of the program that is to be executed, but pass 0 as the new execution time. This will remove the previous request entry and will not create a new one. After an event has occurred, it will be removed from the system's list of execution requests.

Related resources:

Discuss

Discuss this article. Here you can write your comments and read comments of other developers.
Rate this article:     Poor Excellent    
 12345 
© 2001-2005 Pocket PC Developer Network, a division of Spb Software House