views:

307

answers:

1

Hello,

I'm trying to have an auto message reply in Windows Mobile. I'm using MessageInterceptor class which seems to work first time. But it doesn't seems to work for seconds message! not sure if I have to have infinite loop. I don't have a whole lot of experience with Windows Mobile development so please suggest best way.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;


namespace TextMessage3
{
    public partial class Form1 : Form
    {

        protected MessageInterceptor smsInterceptor = null;

        public Form1()
        {
            InitializeComponent();
            debugTxt.Text = "Calling Form cs";
            //Receiving text message
            MessageInterceptor interceptor = new MessageInterceptor(InterceptionAction.NotifyandDelete);
            interceptor.MessageReceived += SmsInterceptor_MessageReceived;                  
        }

        public void SmsInterceptor_MessageReceived(object sender, 
         MessageInterceptorEventArgs e)
        {
              SmsMessage msg = new SmsMessage();
              msg.To.Add(new Recipient("James", "+16044352345"));
              msg.Body = "Congrats, it works!";
              msg.Send();
              //Receiving text message
              MessageInterceptor interceptor = new MessageInterceptor(InterceptionAction.NotifyAndDelete);
              interceptor.MessageReceived += SmsInterceptor_MessageReceived;   

        }



    }
}

Thanks,

Tam

+5  A: 

It looks like your MessageInteceptor class is getting disposed before you are getting your second message because the only reference to the object is going away once you leave the constructor or your event handler. Rather than creating a new object each time you receive a message, just create one in your constructor and set it to you member variable. Each time a message is received your SmsInterceptor_MessageReceived function should get called.

public partial class Form1 : Form
    {

        protected MessageInterceptor smsInterceptor = null;

        public Form1()
        {
            InitializeComponent();
            debugTxt.Text = "Calling Form cs";
            //Receiving text message
            this.smsInterceptor  = new MessageInterceptor(InterceptionAction.NotifyandDelete);
            this.smsInterceptor.MessageReceived += this.SmsInterceptor_MessageReceived;                  
        }

        public void SmsInterceptor_MessageReceived(object sender, 
         MessageInterceptorEventArgs e)
        {
              SmsMessage msg = new SmsMessage();
              msg.To.Add(new Recipient("James", "+16044352345"));
              msg.Body = "Congrats, it works!";
              msg.Send();  
        }
    }
heavyd