MessageInterceptor中的一些延迟处理消息

时间:2011-12-15 12:59:14

标签: c# windows-mobile sms

对不起,我的英语不太好。

我是编程世界的新手,并试图在Windows Mobile 6.5.3上使用messageinterceptor创建一个应用程序。 但是当我向手机发送短信时,在处理短信之前有大约30秒或更长时间的延迟,这些短信是否包含关键字。

在决定尝试创建自己的应用程序之前,我读了几个来源,但是这些源代码使用的是Windows窗体(GUI),而不是使用Windows窗体,我让它在控制台模式下运行。

这是代码:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile;
using System.IO;

namespace PenerimaPesan
{
    class Program
    {

        static void Main(string[] args)
        {


            string applicationID;
            applicationID = "tracker";

            MessageInterceptor pesanmasuk = null;
            pesanmasuk = new MessageInterceptor();
            pesanmasuk.EnableApplicationLauncher(applicationID);


            if (MessageInterceptor.IsApplicationLauncherEnabled(applicationID))
            {
                string keyword;
                StreamReader key = new StreamReader(@"\Windows\conf.txt");
                string data = key.ReadToEnd();
                string[] isi = data.Split(new char[] { '\n' });
                keyword = isi[1];
                keyword = keyword.Replace(" ", "");

                pesanmasuk = new MessageInterceptor(InterceptionAction.NotifyAndDelete, false);
                pesanmasuk.MessageCondition = new MessageCondition(MessageProperty.Body, MessagePropertyComparisonType.StartsWith, ""+keyword);
                pesanmasuk.MessageReceived += new MessageInterceptorEventHandler(pesanmasuk_MessageReceived);
            }
        }

        static void pesanmasuk_MessageReceived(object sender, MessageInterceptorEventArgs e)
        {
            SmsMessage pesan = e.Message as SmsMessage;

            if (pesan != null)
            {
                string perintah;
                string[] command = pesan.Body.Split(new char[] { '.' });
                perintah = command[1];

                if (perintah == "helo")

                /*do some Stuff*/
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

我从未使用MessageInterceptor,所以我决定尝试在我的应用程序中实现此代码。为了测试它,我将 Main 重命名为 Main2 ,然后将其清除以匹配“我的风格”。

无论如何,当我尝试在MessageInterceptor块中包裹using时,我遇到了错误 - 不是因为MessageInterceptor没有实现IDispose,而是因为你宣布了新的实例。

看看你的代码片段:

MessageInterceptor pesanmasuk = new MessageInterceptor();
pesanmasuk.EnableApplicationLauncher(applicationID);
if (MessageInterceptor.IsApplicationLauncherEnabled(applicationID)) {
  string keyword;
  StreamReader key = new StreamReader(@"\Windows\conf.txt");
  string data = key.ReadToEnd();
  string[] isi = data.Split(new char[] { '\n' });
  keyword = isi[1];
  keyword = keyword.Replace(" ", "");
  pesanmasuk = new MessageInterceptor(InterceptionAction.NotifyAndDelete, false);
好的,就在那里。 停止。您创建了pesanmasuk变量的新实例,设置了属性,进行了一些检查,使用了文本文件中的数据,然后......

创建了pesanmasuk变量的新实例。

以前的所有设置现已被删除。

我猜你的第一个实例正在运行,也许第二个实例必须等待第一个实例超时才能创建它。

此时,我有兴趣了解如何在MSDN上使用此MessageInterceptor,在那里查看示例,并提出了这个[未经测试的]版本:

static void Main2(string[] args) {
  const string stackOverflowUrl = @"http://stackoverflow.com/questions/8520488/some-delay-processing-message-in-messageinterceptor";
  string empty = String.Empty;
  StreamReader key = new StreamReader(@"\Windows\conf.txt");
  string data = key.ReadToEnd();
  string[] lines = data.Split(new char[] { '\n' });
  string keyword = lines[1].Replace(" ", empty);
  string applicationID = "trackingApplication";
  using (MessageInterceptor smsInterceptor = new MessageInterceptor(applicationID, false)) {
    smsInterceptor.InterceptionAction = InterceptionAction.NotifyAndDelete;
    smsInterceptor.MessageCondition = new MessageCondition(MessageProperty.Body, MessagePropertyComparisonType.StartsWith, empty + keyword);
    smsInterceptor.MessageReceived += new MessageInterceptorEventHandler(Intercept_MessageReceived);
    smsInterceptor.EnableApplicationLauncher(applicationID);
    if (MessageInterceptor.IsApplicationLauncherEnabled(applicationID)) {
      // Here, you'd need to launch your Form1 or enable some timer,
      // otherwise the code will return immediately and the MessageInterceptor
      // instance will be disposed of.
    }
    smsInterceptor.MessageReceived -= MessageInterceptorEventHandler;
  }
}

static void Intercept_MessageReceived(object sender, MessageInterceptorEventArgs e) {
  SmsMessage newMessage = e.Message as SmsMessage;
  if (newMessage != null) {
    Console.WriteLine("From: {0}", newMessage.From.Address);
    Console.WriteLine("Body: {0}", newMessage.Body);
    string[] command = newMessage.Body.Split(new char[] { '.' });
    string line = command[1];
    if (line == "helo") {
      /*do some Stuff*/
    }
  }
}

我希望这会有所帮助,但请记住,我从未真正使用过此控件,而且我的代码尚未经过测试。

相关问题