Click here to Skip to main content
15,885,760 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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)

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

    }
}
Posted
Updated 19-Jan-13 21:10pm
v6
Comments
Sergey Alexandrovich Kryukov 20-Jan-13 1:46am    
Not enough information even to discuss anything. Not really a valid question.
You need to create and show some code sample, provide comprehensive issue report, use the debugger before you ask...
—SA

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900