Welcome Guest!
Create Account | Login
Locator+ Code:

FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Click here to receive your FREE subscription to Visual Studio Magazine

email article
printer friendly

Build a Simple Device Application
Avoid common pitfalls with these step-by-step instructions for writing your first device application.
by Dan Fergus

June 19, 2007

Now that you're familiar with what's available and what's required in Microsoft's device development space it's time to build an application. Your first application will be a device application that, like a Windows application in the desktop framework, will have a UI component. You will use Windows Mobile 6 to create this application.


Other types of projects for Smart Device Development include a console application, a control and a class library. You'll explore these project types in later installments of this column.

By design, Pocket PCs and Smartphones are disconnected, that is they don't always have a direct connection to a network. As you begin to design and write your device application, you must plan for a new paradigm: connectivity. The functionality of your application will include the ability to check if a connection exists.

Getting Started
Open Visual Studio, and create a new project. Next, add a button control and a label. You can use Figure 1 as a guide.

First, you must check if you have a connection to the internet. You can use several methods to perform this check. For this application, use objects from the System.Net namespace. Call the code shown in Listing 1 from the button click event.

This method takes no parameters; the required fields are populated in it. As a test site, attempt a connection to the Microsoft Web site where credentials are not required. Notice in Listing 1 that if you get back a valid HttpWebResponse and it has a Status of OK, then True is returned from your method. Any other return from your call to GetResponse will be False.

Here is the code that you must add to the event handler for your button click:

if (CheckConnectionState())
    lblConnectedState.Text = "Yea, we can see out!";
    lblConnectedState.Text = "No Connection found.";

Next, run your application using the Windows Mobile 6 Professional Emulator, and tap on your button. Unless you've been playing with the emulator, you should see your label change to "No connection found" (see Figure 2). By default, your emulator is not connected to a network. You need to "cradle" the emulator to get a connection, just as you would your Windows-based phone.

To cradle the emulator, select "Device Emulator Manager…" from the Tools menu in Visual Studio. When you open this dialog, your list will show a little green arrow in the circle next to the Professional Emulator (see Figure 3). This icon tells you that the device is running and connected. Right-click the Windows Mobile 6 Professional Emulator, and select Cradle from the Context menu. Your emulator will connect to your computer through Active Sync.

You may encounter two problems when connecting your emulator. First, ActiveSync supports only one connected device at a time. If you have a Smartphone or another Pocket PC device connected to your system, then you will need to remove it before the emulator can connect. Second, you may need to change a setting in ActiveSync on your desktop. Open ActiveSync, and select "Connection Setting…" from the File menu. For the emulator to connect, ActiveSync must connect to your emulator through direct memory access (DMA). Check the box next to "Allow connections to one of the following:" and select DMA in the combo box. Figure 4 shows your Device Emulator Manager once you have cradled the emulator.

After you've completed this, run your application and tap the Check Connection button. You should receive a message saying that you have a network connection. But, what about that delay when the application is checking the connection? Is it working? Is it broken? You tap the button and nothing happens for up to 20 seconds (that's the timeout that you used in the code).

You need a UI clue to tell your users that an action is occurring (users love that kind of thing). In the button event, you can set the current cursor to WaitCursor, and then reset it before you leave the event. But what if the event raises an exception? You may leave the method without resetting the cursor to Normal. To prevent this, create the new class shown in Listing 2. Listing 2 uses the IDisposable interface to allow you to write unique code for the button event:

using (CursorWait cw = new CursorWait())
    if (CheckConnectionState())
        lblConnectedState.Text = "Yea, we can see out!";
        lblConnectedState.Text = "No Connection found.";

Finally, if your CheckConnectionState method raises an exception, the using statement will make sure that the Dispose method of the CursorWait class is called — thereby resetting the cursor to its default state.

You've written your first application; congratulations! In addition, you have learned some tricks for resolving problems that tend to frustrate developers who are moving from desktop to device development.

About the Author
Dan Fergus is the principal technologist at Forest Software Group, which provides consulting and contracting services to companies throughout the U.S. He is a Device Application Development MVP, a trainer, and the co-author of The Definitive Guide to the .NET Compact Framework. Fergus has also written many articles, is a frequent speaker at conferences and user groups, and is a contributing editor for Visual Studio Magazine. Contact him at danf@forestsoftwaregroup.com.

Back to top

Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTPOnline Home