locked
Message Interceptor Not Working RRS feed

  • Question

  • I'm working on an application which will send out a text message periodically and then listen for a reply. I'm using windows mobile 6 and .net3.5 with Message Interceptor to listen for the incoming messages.

    The code for the application is shown below. There is an infinite while loop which runs and is exited if a message is intercepted. I would prefer to use a timer but when the timer which is commented out in the below code is used instead of the while loop the application sends 1 text and then closes. With the current set up it runs correctly but does not intercept any messages.

    Originally this was a form application but now needs to run in the background and so I have moved it over to a console application. This caused many issues (switching form timer to threading timers etc.) 

    If anyone can see why this isn't working please help out this has been giving me hassle for about 3 days and I think i'm starting to go insane :(

     

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using Microsoft.WindowsMobile;
    using Microsoft.WindowsMobile.PocketOutlook;
    using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
    using System.Windows.Forms;
    namespace HPSMSConnection
    {
      public class Program
      {
        Stopwatch stpw = new Stopwatch();
        string[] numbersArray = new string[10];
        int selectNumber = 0;
        TimeSpan limit;
        TimeSpan passed;
        bool canEnd = false;
        public static void Main(string[] args)
        {
          Program classInstance = new Program();
    
          MessageInterceptor interceptor = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true);
          interceptor.MessageReceived += new MessageInterceptorEventHandler(classInstance.SmsInterceptor_MessageReceived);
          
          //Create a file on the device with a list of phone numbers
          //this just has the emulator number 3 times.
          using (StreamWriter sw = File.CreateText(@"\Numbers.txt"))
          {
            sw.WriteLine("14254448851");
            sw.WriteLine("14254448851");
            sw.WriteLine("14254448851");
          }
          
          //Streamreader reads numbers from a textfile Numbers.txt 
          //is stored on the device
          using (StreamReader sr = File.OpenText(@"\Numbers.txt"))
          {
            string s = "";
            int i = 0;
            while ((s = sr.ReadLine()) != null)
            {
              //phone numbers stored in an array
              classInstance.numbersArray[i] = s;
              i++;
            }
          }
          
          classInstance.SendSMSMessage();
          
          /*Origionally I had a timer instead of an infinte loop which would be better but neither method works
           TimerCallback tcb = new TimerCallback(classInstance.OnTimerEvent);
           System.Threading.Timer aTimer = new System.Threading.Timer(tcb, null, 0, 10000);
          */
          while (classInstance.canEnd == false) {
    
            classInstance.OnTimerEvent();
          
          }
        
        
        }
    
    
        void SendSMSMessage()
        {
    
          SmsMessage smsMessage = new SmsMessage();
    
          
          smsMessage.Body = "Testing Text";
          smsMessage.To.Add(new Recipient("John Doe", numbersArray[selectNumber]));
          smsMessage.RequestDeliveryReport = true;
          smsMessage.Send();
    
          selectNumber++;
          
          //stopwatch started to record time since last text sent
          stpw.Start();
    
        }
    
    
        public void OnTimerEvent()
        {
          // Program classInstance = new Program();
    
          //This is to ensure there is at least 
          //a 10 second gap in the text sending
          limit = new TimeSpan(00, 00, 10);
          passed = stpw.Elapsed;
          if (passed > limit)
          {
            stpw.Stop();
            stpw.Reset();
            SendSMSMessage();
          }
        }
    
        
        void SmsInterceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
        {
          //message recieved so stop the timer and the stopwatch
          stpw.Stop();
          canEnd = true;
          SmsMessage msg = e.Message as SmsMessage;
          if (msg != null)
          {
            string msgBody = msg.Body.ToString();
            //writes data to a file on the phone called test.txt
            string path = @"\test.txt";
    
            using (StreamWriter sw = File.CreateText(path))
            {
              sw.Write(msgBody);
    
            }
            //Just to check if the Interceptor is called
            MessageBox.Show("Reached");
    
            
          }   
    
        }
      }
    }
    

     

    David

    Wednesday, April 13, 2011 5:47 PM

Answers

All replies