Windows Mobile C#3.5应用程序事件不会触发 [英] Windows Mobile C# 3.5 application event not fires

查看:76
本文介绍了Windows Mobile C#3.5应用程序事件不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是活动编程新手。我需要编写一个带有事件的Windows Mobile小应用程序。我需要连续运行这个应用程序,每当事件触发时,它应该将日志记录写入文本文件。这个应用程序应该在屏幕后面运行(没有我们的界面)。问题是这个事件只触发,如果我放一个表单并将该事件绑定到表单或放置一个messageBox。另一个明智的事件是这个事件不会发生......它是一个聚焦问题吗?请帮我解决这个问题。



当我在while循环中取消注释测试消息时,事件会正常触发。



这是我的代码。(我只包括我的代码部分。GPSReader类是下载的一个)



Hi,

I am new to event programming. I need to write a Windows Mobile small application with an event. I need to run this app continuously and whenever that event fires, it should write a log record to a text file. this application should run on behind the screen (without our interface). The problem is this event fires only, if i put a form and bind that event to the form or put a messageBox. other wise this event doesn''t fire... is it a focusing problem? Please help me on this issue.

When I un-comment the Test Message inside the while loop, then the event fires correctly.

Here is my code.(I have included only my code part. "GPSReader" class is a downloaded one)

using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading;

namespace MYGPSTest
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        private static GPSReader gpsRead;
        private static string gpsString = string.Empty;
        private static string strPort = "COM3:";
        private static double lat = 0;
        private static double lngi = 0;

        [MTAThread]
        static void Main()
        {
            GPSCapture();
            Application.Exit();
        }

        private static void GPSCapture()
        {

            gpsRead = new GPSReader();
            gpsRead.OnGPSMessage += new GPSEventHandler(gps_OnGPSMessage);
            gpsRead.BaudRate = 9600;
            gpsRead.PortName = strPort;
            gpsRead.StartRead();

            DateTime startTime = DateTime.Now;

            while (startTime.AddSeconds(18000) > DateTime.Now)//To keep this program running 5 hrs
            {

                //MessageBox.Show("Test Message"); 

                Thread.Sleep(10000);

                WriteToLog("Save to Log (GPSCapture): " + lngi.ToString().Trim() + ", " + lat.ToString().Trim() + " : " + DateTime.Now.ToString("HH:mm:ss").Trim());

            }
            gpsRead.StopRead();
        }

        private static void gps_OnGPSMessage(object sender, GPSEventArgs args)
        {
            if (args != null)
            {
                lat = args.Lat;
                lngi = args.Lon;

                WriteToLog("Save to Log (gps_OnGPSMessage): " + lngi.ToString().Trim() + ", " + lat.ToString().Trim() + " : " + DateTime.Now.ToString("HH:mm:ss").Trim());
            }
        }

        internal static void WriteToLog(string message)
        {
            StreamWriter streamWriter = null;
            try
            {
                string dPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\Log.txt";

                streamWriter = new StreamWriter(dPath, true);
                streamWriter.WriteLine(message);
                streamWriter.WriteLine("");
                streamWriter.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            finally
            {
                if (streamWriter != null)
                    streamWriter.Dispose();
            }
        }

    }
}

推荐答案



我找到了解决方案。但是不知道它是一个好的解决方案还是一个坏的解决方案。



我把Application.DoEvents();睡前循环内部。现在它正常工作。
Hi,
I found a solution. but do not know where it is a good solution or a bad one.

I put "Application.DoEvents();" inside the loop before sleep. now it is working correctly.


这篇关于Windows Mobile C#3.5应用程序事件不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆