Nadav and the wonders of the web


Archive for January, 2008

How to: Run a program on start up on Windows mobile

Wednesday, January 30th, 2008

Thinking about how to run a program on the Smart Phone’s boot sequence got me searching and I did find the answer but it was not as easy and as clear to find as I hoped. That is the reason that i came to write this post.

The very simple and easy way to do this is to add a shortcut to the program we want to run under \Windows\StartUp

Yes its that simple.

There might be a different way to do this using the registry, but i couldn’t find one and quite frankly, I don’t see the need for it.

What you might want to do is add your created shortcut to the your installation CAB[I will try and have a different post regarding this one, it is a big subject.] under this folder so it will be done on installment and you won’t have to do it programmatically in a later phase. On the other hand, doing it programmatically leaves you the option to have this as a setting in your application that the user can set. I will leave the choice down to you. =]

Hope this was helpful!

Nadav

How to: Unit test events

Tuesday, January 22nd, 2008

When we come to write unit tests we stumble upon many different problems with many different parts of our code, because no matter how hard you try, your code is never as simple as the examples around the web. If it’s singletons, static methods, private members that need mocking, or even events that need to be checked.

lately I spent some time writing unit tests and fumbled around with many of this problems. There are many articles around the web telling you the problem is not that you don’t know how to test this parts, it is that it shouldn’t exist in the first place. I really don’t like this answer, saying I shouldn’t write my code this way because there is a better one is a valid argument, but telling me I HAVE to change my code in order for it to work is usually not true. Some places don’t say this but still, most don’t give you a solution.

After this brief intro I will go into saying that I will try in a few different posts to clear up some of the problems in unit testing, mainly the ways to overcome them.

Lets start =]

As the title states, in this post I will show how to unit test events.

What we are going to do here is use an Anonymous delegate in order to catch the event inside our test.

The code: (look afterwards for the explanation and break down)

[TestFixture]
public class SettingsFixture
{
    [Test]
    public void ChangeSettingFiresSettingsChangedEvent()
    {
        // success variable
        bool success = false;

        // new setting
        string key = "SomeKey";
        string value = "SomeValue";

        // set a new instance
        SettingsManager settingsManager = new SettingsManager();

        // register to the event
        settingsManager.SettingsChanged += delegate(object sender,
          EventArgs e)
        {
            // event raise, test successfull
            success = true;
        };

        // update a setting to raise the event
        settingsManager.UpdateSetting(key,value);

        Assert.True(success);
    }
}

So what we have here?

We have a TestFixture with a single Test called:

ChangeSettingFiresSettingsChangedEvent

Change Setting Fires SettingsChanged Event.

First, we setup the success variable which we will commit later as the test’s result.

Second, set up our "new" setting, if we would use a special Type to submit to the UpdateSetting method, ISetting lets say, this will be the place to Mock(Mocking will be discussed in a future post, I will link to it then) it.

Third, setup a new instance of our SettingsManager.

Fourth, register to the SettingsChanged event as we usually will, with one big difference. instead of using a new instance of the right delegate we use an Anonymous delegate, look at the code for the syntax, it is very simple.

Fifth, call UpdateSetting with our "new" setting so the event will be raised.

Sixth, commit the result, hoping that it all went as planned ;].

Seventh, show off to your friends and go home, it’s late! (somewhere in the world…)

Hope it was clear enough.

A nice post about this:

Just to clarify: I am using C#(if you haven’t noticed yet) with NUnit.

See you next time =].

Nadav

How To: Bring your program to the foreground on WM6

Sunday, January 13th, 2008

This topic is one I have been spending some time studying.
How do you get your program to the foreground when a certain event accurse ?

The answer is quite simple actually, you just need to call a UI component. like:

MessageBox.Show("Show Me");

And it will show the top most form in your application. Another thing you can do is call a form with Show().

Now, for a little bonus I have for you the…

How to send it back to the background!

What we need to do here is to call Hide() on all the visible forms in our application, this will essentially cause all our UI to go to the background and will bring back the last active application.

A little hint, you might want to use what you learned, and combine it with the previous post ;].

Nadav

How to: Using the State and Notification broker on WM6

Sunday, January 13th, 2008

Sometimes we want to be notified of different events that happen in the operating system, even when we are not “looking”. the State and Notification broker is built especially for this.

I will give an example of how to be notified when the current active application changes and how to handle it.

First create the correct SystemState member:

SystemState _activeProgram = new SystemState(SystemProperty.ActiveApplication);

Now set the event handler on your program initiation:

_activeProgram.Changed += new ChangeEventHandler(_activeProgram_Changed);

Now handle it ;]:

void _activeProgram_Changed(object sender, ChangeEventArgs args)
{
    MessageBox.Show(((string)args.NewValue));
}

You would want to know a few interesting things:

  1. The NewValue string is formatted in this way:

    PreviousActiveProgramNameCurrentActiveProgramName

    So, you might want to use StartsWith\EndsWith methods in order to check them ;].

  2. It seams that every UI involved action(MessageBox, Form, etc.) causes your own application to come back from the background(if it was there) and show itself ;]. while other non-UI related actions does not cause this. This only a major “feeling” i got from my little workings with this so far, i checked this with a Thread.Sleep that did not cause my application to come to the foreground.

Hope this helps.

Nadav

Installing Windows Live Writer on Windows server 2003

Sunday, January 13th, 2008

As I am using Microsoft Visual Studio 2005 for most of my coding and I wanted it to look nice when I copy it to my blog, I decided to use WLW(Windows live writer).

BUT as I am using Windows server 2003, it was not possible. ow no.

Don’t fret ! here comes Google for the rescue. after searching very little a found very nice post about how to overcome this.

If you just want to download -

This should work for most if not all people, myself? I couldn’t get anything running but the , hope you get it to work for you, if not might want to try what worked for me.

Tell me what worked for you in the comments !

Hope this helps you like it helped me ! =]

Update:
after installing the Beta I tried again and was able to install the 2008 version. Hooray !
(I just used the installer and when it finished[with no mention I will add] it was installed.)

Update 2:
Windows update managed to upgrade the software just fine !

Nadav

How to run a system command from a PHP page.

Wednesday, January 2nd, 2008

Using PHP to run system command on the server side can come in very useful in many occasions.
Some examples:

  • Using an online storage service for your site that doesn’t give you shell access.
  • Running this during an application logic.

I will give here the main idea of this technique and a few code examples of useful commands to use with it.

Downloading a file to the server:
When I upgrade my WordPress installation I don’t want to download all the files to my personal PC and then re-upload them to the server so what I do is, I use this command to download the files:

<?php system(‘wget http://url-to-download-location/filename.zip’); ?>

Then I unzip the file using this command:

<?php system(‘unzip -o filename.zip’); ?>

The -o means it will overwrite existing files without asking so I would use that one with caution. be warned.

As you probably gathered by now the way to run system commands from php is very simple, just put the code:

<?php system(‘command’); ?>

And that is it. all you have to do now is go to your browser and access the URL of the php file.

http://yoursite/yourPHPfile.php

And the command will run =]

’till next time, have a good one =]

Nadav…

Jajah is the VoIP player that brought you web-activated telephony.