2

I need help with my program. So I have com port connection ( gps device ) and when the program starts I want to be able to show Form2 until gps eventhandler gets triggerd. And same thing when device starts to send me data, there is some time when device is not available to give me data. How can I know in that time that I need to show again Form2?

This is a bit complicated to explain.

Here is what I have done so far. So when Form1 loads I am showing a Form2 screen which basically says that I am waiting for signal to shown up. Now how can I put this in some sort of loop or something like that which will be always checking for eventhandler to be triggerd and in mean time I am showing this form2.

If you need more explanation let me know.

So here is the code:

private void Form1_Load(object sender, EventArgs e)
{
  //open the com port when loading form
  comport.Open();

  Form2 my_form2 = new Form2();
  my_form2.ShowDialog();
}

This is the event handler for GPS signal:

GPS.PositionReceived += new NmeaInterpreter.PositionReceivedEventHandler(GPS_PositionReceived);

And this is GPS function:

private void GPS_PositionReceived(string Lat, string Lon)
{
arrLon = Lon.Split(new char[] { '°', '"' }, StringSplitOptions.RemoveEmptyEntries);
dblLon = double.Parse(arrLon[0]) + double.Parse(arrLon[1], new System.Globalization.NumberFormatInfo()) / 60;
deciLon = arrLon[2] == "E" ? dblLon : -dblLon;

//some more code 
}
user123_456
  • 5,635
  • 26
  • 84
  • 140
  • 1
    So why not just close the form when the event handler triggers? that is their whole purpose. They run code when something triggers. Having an indefinate loop that waits for a state change is a bad idea. – IAmGroot May 18 '12 at 14:30
  • @Doomsknight and how to show it again when it doesn't triggers. For example car went into the tunnel signal is lost but I need to activate form2 which will say no signal – user123_456 May 18 '12 at 14:32
  • @Doomsknight maybe I can somehow check if event is not triggerd for some seconds? – user123_456 May 18 '12 at 14:33

1 Answers1

1

So why not just close the form when the event handler triggers? that is their whole purpose.

They run code when something triggers.

Having an indefinate loop that waits for a state change is a bad idea.

private void GPS_PositionReceived(string Lat, string Lon)
{
    arrLon = Lon.Split(new char[] { '°', '"' }, StringSplitOptions.RemoveEmptyEntries);
    dblLon = double.Parse(arrLon[0]) + double.Parse(arrLon[1], new System.Globalization.NumberFormatInfo()) / 60;
    deciLon = arrLon[2] == "E" ? dblLon : -dblLon;

    //some more code 

    // LOAD FORM 1
    // CLOSE THIS FORM (FORM 2)
}
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • Okay this is good when the signal shows. But what when it disapears? – user123_456 May 18 '12 at 14:34
  • Can you not just add a text component on form 1 that specifies that the signal has been lost? I assume there is some code that is trying to get the GPS coordinates every 5 seconds or so? If its lost, just add a message on screen, and if its obtained, hide the message. – IAmGroot May 18 '12 at 14:37
  • I just need specific form to be show. I mean you are thinking right. But this is what it needs to be done. Yes this is for getting signal every 5 sec. But problem occurs because if signal is not getting how to check that state? Is it possible to check event state? – user123_456 May 18 '12 at 14:39
  • Is there a failed event handler for the GPS object? Maybe you could add a method to that listener to call up your form. – IAmGroot May 18 '12 at 14:42
  • you have `new NmeaInterpreter.PositionReceivedEventHandler`. Is there a `new NmeaInterpreter.PositionFailedEventHandler` (or something similar)? – IAmGroot May 18 '12 at 14:46
  • No I don't have that, I have found this question but I don't get it quite good, maybe you will be more lucky then me: http://stackoverflow.com/questions/672638/use-of-null-check-in-event-handler – user123_456 May 18 '12 at 14:47
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11431/discussion-between-doomsknight-and-denonth) – IAmGroot May 18 '12 at 14:53