Videos | Podcasts

How to start application by scheduled time with Windows mobile.
RRaveen
Published Date: 3/30/2009 7:10:16 PM
Views: 1556

Abstract:
In this article we will focus on how to start an application or system by the schedule time in windows mobile devices. In this scenario I’m going to work with .net compact framework 3.5 and Windows mobile latest Device 6.1.4 to test it.

Introduction:

Actually this is not hard to do in windows mobile, but there are some limitations because .net compact framework is not like the full .net framework.  Since we are talking about scheduling, Time is the key factor.   In Window CE operating system SystemFileTime is usually preferred rather than normal date and time.

Prerequisites:

1. Windows Mobile 6 SDK
2. Windows Mobile 6 Professional Emulator.
3. Visual Studio 2008

Overview:

First we want to understand SystemFileTime because we’re going use this to schedule time implementation.  So what is SystemFiletime?  It is related to Operating system time and is also a structure to maintain the time object in the memory. In other words it contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).  You can find more details on FileTime here http://msdn.microsoft.com/en-us/library/ms724284(VS.85).aspx.

Implementation:

Now we move to our scenario:  I want to start an application at a scheduled time using the parent System specifically I’m going to start pocket word at a scheduled time. For this I created a very simple user interface.


    
User Interface:

It's a very simple GUI where I’ve added a simple menu with File and about. Under the File I have a submenu called Start. When I click the Start I’m going to set my schedule time to FileTme and then to another Operating system function.     

Before coding we must understand the Platform invoke services in .Net framework and .Net compact frame work.  Next I’m giving a small intro with details about P/Invoke.

What is the P/Invoke?

.Net technology has wonderful feature access operating system functions within it.  When we try to access the OS function within the Managed code, Microsoft calls the P/Invoke service. Look below:


    
Platform Invoke Service.

Click the link below to get more info on P/Invoke.

http://msdn.microsoft.com/en-us/library/h50dxzwx.aspx

I hope you now understand P/Invoke.   Now you can raise another question:  How do we call the function within.Net. This is very simple, just view the next example and you’ll see the answer to that question.
    
First I’m going to write code to P/invoke some function to use my example.


static class NativeMethods
        {
        [DllImport("CoreDLL.dll",SetLastError=true)]
        public static extern int CeRunAppAtTime(string application, SystemTime startTime);

        [DllImport("CoreDLL.dll", SetLastError = true)]
        public static extern int FileTimeToSystemTime(ref long lpFileTime, SystemTime lpSystemTime);

        [DllImport("CoreDLL.dll", SetLastError = true)]
        public static extern int FileTimeToLocalFileTime(ref long lpFileTime, ref long lpLocalFileTime);

    }

    [StructLayout(LayoutKind.Sequential)]
    public class SystemTime
    {
        public ushort wYear;
        public ushort wMonth;
        public ushort wDayOfWeek;
        public ushort wDay;
        public ushort wHour;
        public ushort wMinute;
        public ushort wSecond;
        public ushort wMilliseconds;
}

Notice how we access the operating system function within the .net by the P/Invoke services.

        [DllImport("CoreDLL.dll",SetLastError=true)]
        public static extern int CeRunAppAtTime(string application, SystemTime startTime);


    [DllImport("CoreDLL.dll",SetLastError=true)]-importing dll
public static extern int CeRunAppAtTime(string application, SystemTime startTime);-


we must use the static extern keyword for this purpose but that’s not difficult if you know the operating system function name.   Visit the site below to view  the .net syntax.

http://www.pinvoke.net/

Now I’m going to move to my example.

I’m going to initilize a few variables to use my upcoming code.

// start time object
SystemTime systemStartTime = null;
// delegate for update control
private delegate void ShowTimer(string time);
// timer for display
        Timer timer = null;
        DateTime startTime = DateTime.MinValue;
        int timerStop = 0;
now I’m going to write very simple code for the Start submenu click.
    private void menuItemStart_Click(object sender, EventArgs e)
        {
            startTime = DateTime.Now.AddSeconds(30);
            if (timer == null)
            {
                timer = new Timer();
                timer.Enabled = true;
                timer.Interval = 1000;
                timer.Tick += new EventHandler(timer_Tick);
            }
            long fileStartTime = startTime.ToFileTime();
            long localFileStartTime = 0;
            NativeMethods.FileTimeToLocalFileTime(ref fileStartTime, ref localFileStartTime);

            if (systemStartTime == null)
            {
                systemStartTime = new SystemTime();
            }
            // change normal time to filesystem time
            NativeMethods.FileTimeToSystemTime(ref localFileStartTime, systemStartTime);
            // set the start application Runtime with schedule time
            NativeMethods.CeRunAppAtTime(@"\Windows\pword.exe", systemStartTime);
        }

This is basic level code I’ve written for the menu click event but here  you must understand some key points.
First I ‘m setting start time, then I’m converting normal time to filetime on the line long fileStartTime = startTime.ToFileTime(); then I’m going to get the localFiletime using the Filetime.


NativeMethods.FileTimeToLocalFileTime(ref fileStartTime, ref localFileStartTime);


I’m going to get localfiletime using the FileTimeToLocalFileTime function. Now we call another function, set my application which I want to start and when I want to start it.
        
  
NativeMethods.CeRunAppAtTime(@"\Windows\pword.exe", systemStartTime);


Now view the results:



I set 30 seconds to schedule time to lunch IE after 30 second resalt are like this.

Final Touch:

I discussed how to launch and start an external application by the schedule based time.  It’s very useful when we need work in autolaunch scenarios and I hope this article is useful to mobile developers.

References:

1. http://msdn.microsoft.com/en-us/library/ms913957.aspx
2. http://msdn.microsoft.com/en-us/library/ms724280(VS.85).aspx
3. http://msdn.microsoft.com/en-us/library/ms724277(VS.85).aspx
4. http://msdn.microsoft.com/en-us/library/ms724950(VS.85).aspx

[Download Sample]

Author:

My name is RRaveen, currently living and working in Singapore. Ii am highly focusing on to provide better Solutions to business problems which are commonly an organization facing, retrieve information on time using information technologies.Since i loves windows mobile technology design and implementation business applications which is based on C#.And my other specializations are Asp.net,WPF and WCF .
i have been working on many back ends which are Mysql,Sqlserver and Oracle/PL/SQL/TOAD hands on experience more than 3 and a half year.



Did you like this article?
kick it on DotNetKicks.com Submit
Similar Articles

How to Access the Weather Information from Yahoo in the Windows Mobile Device

How to Run Multi Threaded Windows Mobile Application with High Performance

How to Access SQL Server 2005 from the Windows Mobile Devices

How to Access GPS in Window Mobile Devices Part 2

How to access GPS in Windows Mobile devices

Enter Comment/Feedback

 
 
 
 
 

Comments/Feedbacks