views:

102

answers:

3

I have a basic SMS interception application on a windows mobile phone, currently this is a console application that hooks up the MessageReceived event of the MessageInterceptor class.

Once i've done that my program runs to compleation, my Phone will then display the 'busy circle' untill I do something else but my event handlers still get fired when i recive an SMS that match my filter.

I've tried a few other approches;

  • ManagedServicesWM project from Codeplex - this was overly complicated for what I needed and turned out to be an infinite loop with a Thead.Sleep(200) so was also a waist of battery power for my applicaion.
  • Doing a Console.ReadLine(); - but it seems that standard input is null for WM console apps so this did nothing.
  • Using a forms applicaion - this just made my basic applicaion more complex and multithreaded with no gain, and gave my a usless form.

But none of these alternitives seem as good as my hanging console app, but that doesn't feel like the right anwser ether.

So I was wondering if any one had some other ideas on ways to implement this style of app and if I may find that at some point my application will be ended by the windows memory manager?

A: 

you could try and use OPENNETCF instead - http://www.opennetcf.com/library/sdf/html/8ef93ad6-d839-2c70-695d-04e2679c0c27.htm

Barry
A: 

I don't know that I understand the problem. I get that you want to have an app with no UI that monitors for incoming SMS, but not necessarily what the exact issue you have is.

I would expect the general procedure to look like this:

static class Program
{
    static MessageInterceptor interceptor;

    static void Main()
    {
        Thread listener = new Thread(new ThreadStart(
            delegate
            {
                interceptor = new MessageInterceptor(InterceptionAction.Notify, false);
                interceptor.MessageReceived += new MessageInterceptorEventHandler(OnMessageReceived);
            }));
        listener.IsBackground = true;
        listener.Start();

        EventWaitHandle eh = new EventWaitHandle(false, EventResetMode.AutoReset, "MyShutdownEventName");

        while (!eh.WaitOne(1000, true))
        {
            // do nothing
        }

        interceptor.Dispose();
    }

    static void OnMessageReceived(object sender, MessageInterceptorEventArgs e)
    {
        DoSomething();
    }
}

"But wait, you've got an infinite loop in there, and that's going to kill my battery" you say? Hardly. How do you think WinForms apps work? They have a message pump that, surprise, is an infinite loop. I doubt waking once per second is going to have any impact on your power profile.

If you're really concerned, you could change the timeout to Timeout.Infinite, but any use of Infinite gets immediate negative points from me in a code review. An infinite wait is very, very, very, very rarely applicable outside of a driver (and even then I rarely use them) becasue an infinite wait tells the scheduler you're fine with never shutting down.

ctacke
A: 

After a few months of usage I havn't seen my console application get ended by the windows memory manager and all seems to work fine apart from the "busy circle" when a start the application after a reset.

My battery power also seems unafected, which is better then some of the infinate loop, ManagedServicesWM and OpenNetCF options that reduced battery life considerably.

squig
So what, exactly, was your solution?
ctacke
a hanging console app, It hooks up the required events then 'exits'
squig