Jump to content

Stop program from crashing when it cant find IO (Serial Port...)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
nick3

nick3

    Newbie

  • Members
  • PipPip
  • 29 posts
Hey,

I have a question about how I can stop my program from crashing if I pull out the GPS that im reading from, out of the Serial Port. I have tried to put an try that catches an IOexception and it does trigger (shows my error msg), but it still crashes. I get a "The thread '<No Name>'... has exited with code 0 (0x0)" error. What I want it to do is just give an error msg and then try to get the connection back every few seconds... how can I do that?

/Nick

#2
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
try {
//statements here
}
catch {
//If it does not work, this happens
MessageBox.Show("Error");
}
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#3
QuackWare

QuackWare

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
He said he used a try catch statement already and it didn't work. Make sure in your catch statement you are exiting out of the rest of the code that could possibly be run (But would crash due to the failure with the program).

Using a return; statement or something similar would do the trick.

#4
While(!EOF)

While(!EOF)

    Newbie

  • Members
  • PipPip
  • 21 posts
You could use a finally block in your try catch statement. This could be done by marking when you throw an exception or not. Finally blocks always execute regardless if the catches catch anything or not. In other words:


bool caught = false; //has the IOException be caught?

while(!causedtoquit) {

caught = false;

try
{
  //...your code to do the IO stuff
}
catch(IOException ioex)
{
    caught = true;
    //handle exception here
}
finally
{
     reConnect(3.00,caught);
}

}
.
.
.
private void reConnect(real timeToReConnect, bool lostComms)
{
   if(lostComms)
  {
   
  while(!connected)
  { 
        //reconnect code here
       System.Threading.Thread.Sleep(timeToReConnect * 1000);
  }  
}
}

maybe something like this will do the trick. This was just off the top of my head so some rearranging probably needs to be done. ;)