Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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();
            }
        }
 
    }
}
Posted 19-Jan-13 19:46pm
Edited 19-Jan-13 22:10pm
SoMad134.4K
v6
Comments
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...
—SA

1 solution

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

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,130
1 DamithSL 5,089
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,647
4 Kornfeld Eliyahu Peter 4,409


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2014
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