As many of you know, I'm a big fan of the Windows Mobile 5.0 & Windows Mobile 6 State and Notifications Broker. It provides a common API for determining Windows Mobile system status and allows you to easily track changes in those status values.
The managed SystemState class provides nearly 150 static properties that allow you to access the values without having create a class instance or deal with the underlying registry structure behind the state values. The thing to remember is that the fact that the SystemState static properties abstract your code from accessing the registry doesn't mean that the work of retrieving the values from the registry isn't happening.
The SystemState static properties do not cache any values, they always read the current value from the registry. This means that every time you use one of these properties in your code that the SystemState class navigates down to the registry key that holds the value of interest, opens the key, retrieves the value and then closes the registry key.
Simple code like the following can actually be pretty expensive.
private void VerifyConnectivity()
{
string message = "";
if (SystemState.ConnectionsCount == 0)
message = "No connections";
else if (SystemState.ConnectionsCount == 1)
message = "1 connection";
else if (SystemState.ConnectionsCount > 1)
message = SystemState.ConnectionsCount.ToString() + " connections";
MessageBox.Show(message);
}
Depending on the value of SystemState.ConnectionsCount, the above code can perform the steps of traversing the registry hierarchy, opening the key, reading the value, and closing the registry key up to 4 times; whereas a simple rewrite performs the registry access only once.
private void VerifyConnectivity()
{
string message = "";
int connectionsCount = SystemState.ConnectionsCount;
if (connectionsCount == 0)
message = "No connections";
else if (connectionsCount == 1)
message = "1 connection";
else if (connectionsCount > 1)
message = connectionsCount.ToString() + " connections";
MessageBox.Show(message);
}
If you're using one of the SystemState values consecutively like this, you absolutely want to cache the value.
The thing that you'll also want to be careful about though, is to not cache the values for too long; remember that the values can change frequently. That said, in implementations like the above code snippets where you're code is operating on a state value from a moment in time, avoid the unnecessary overhead of repeatedly re-accessing the registry.
BTW: I realize that the above code samples are very simplified and could certainly be implemented better than they are but they make a good illustration of the problem J
Posted
Apr 13 2007, 09:31 AM
by
jim-wilson