Click here to Skip to main content
11,427,997 members (61,478 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#

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;
        static void Main()
        private static void GPSCapture()
            gpsRead = new GPSReader();
            gpsRead.OnGPSMessage += new GPSEventHandler(gps_OnGPSMessage);
            gpsRead.BaudRate = 9600;
            gpsRead.PortName = strPort;
            DateTime startTime = DateTime.Now;
            while (startTime.AddSeconds(18000) > DateTime.Now)//To keep this program running 5 hrs
                //MessageBox.Show("Test Message"); 

                WriteToLog("Save to Log (GPSCapture): " + lngi.ToString().Trim() + ", " + lat.ToString().Trim() + " : " + DateTime.Now.ToString("HH:mm:ss").Trim());
        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;
                string dPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\Log.txt";
                streamWriter = new StreamWriter(dPath, true);
            catch (Exception err)
                if (streamWriter != null)
Posted 19-Jan-13 19:46pm
Edited 19-Jan-13 22:10pm
Sergey Alexandrovich Kryukov at 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...

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

Advertise | Privacy | Mobile
Web02 | 2.8.150428.2 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100