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
Stop program from crashing when it cant find IO (Serial Port...)
Started by nick3, Jun 07 2010 08:36 AM
3 replies to this topic
#1
Posted 07 June 2010 - 08:36 AM
|
|
|
#2
Posted 08 June 2010 - 02:40 AM
try {
//statements here
}
catch {
//If it does not work, this happens
MessageBox.Show("Error");
}
//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
Posted 13 June 2010 - 02:01 PM
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.
Using a return; statement or something similar would do the trick.
#4
Posted 13 June 2010 - 06:18 PM
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:
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. ;)
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. ;)


Sign In
Create Account


Back to top









