连接事件以及之后如何处理应用程序

发布于 2024-07-27 12:00:54 字数 589 浏览 1 评论 0原文

我在 Windows 手机上有一个基本的 SMS 拦截应用程序,目前这是一个控制台应用程序,它挂接 MessageInterceptor 类的 MessageReceived 事件。

一旦我完成我的程序运行完成,我的手机将显示“忙圈”,直到我执行其他操作,但当我收到与我的过滤器匹配的短信时,我的事件处理程序仍然会被触发。

我尝试了其他一些方法;

  • Codeplex 的 ManagedServicesWM 项目 - 这对于我的需要来说过于复杂,并且结果是 Thead.Sleep(200) 的无限循环,因此对于我的应用程序来说也是一个电池电量的腰部。
  • 执行 Console.ReadLine(); - 但似乎 WM 控制台应用程序的标准输入为空,因此这没有任何作用。
  • 使用表单应用程序 - 这只会使我的基本应用程序变得更加复杂和多线程,没有任何好处,并给了我一个无用的表单。

但这些替代方案似乎都没有我的悬挂控制台应用程序那么好,但这感觉不像是正确的答案。

所以我想知道是否有人对实现这种风格的应用程序有其他想法,以及我是否会发现在某个时候我的应用程序将被 Windows 内存管理器终止?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故人如初 2024-08-03 12:00:54

我不知道我是否理解这个问题。 我知道您想要一个没有 UI 的应用程序来监视传入的短信,但不一定是您遇到的具体问题。

我希望一般程序看起来像这样:

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();
    }
}

“但是等等,里面有一个无限循环,这会耗尽我的电池”你说? 几乎不。 您认为 WinForms 应用程序如何工作? 他们有一个消息泵,令人惊讶的是,它是一个无限循环。 我怀疑每秒醒来一次会对您的电量状况产生任何影响。

如果您真的很担心,可以将超时更改为 Timeout.Infinite,但是任何使用 Infinite 的行为都会在代码审查中立即给我带来负面影响。 无限等待在驱动程序之外很少适用(即使这样我也很少使用它们),因为无限等待告诉调度程序你可以永远不关闭。

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.

骄兵必败 2024-08-03 12:00:54

使用几个月后,我还没有看到我的控制台应用程序被 Windows 内存管理器结束,除了重置后启动应用程序时的“忙碌循环”之外,一切似乎都工作正常。

我的电池电量似乎也没有受到影响,这比一些大大缩短电池寿命的无限循环、ManagedServicesWM 和 OpenNetCF 选项要好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文