Hey everyone,
I have a warning message in my app saying "Unreachable code detected" - Here's a snippet:
//Files are sometimes not closed quickly enough - these loop variables allow for the system to wait and try again
const int MAX_TRIES = 3;
int loop = 0;
...
public void CreateSheet(Laptop lptp, string file, int id)
{
PointOfRetry:
try
{
subLog.LogMessageToFile("Opening sheet in " + file + " for asset id: " + id);
//Open specified file and set activate worksheed according to asset ID
xlBook = xl.Workbooks.Open(file_path + file, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
xlSheet = xlBook.Worksheets.get_Item(2);
subLog.LogMessageToFile("Creating sheet in " + file + " for asset id: " + id);
//Create a copy of sheet #2 at the end of the workbook
int last = xlBook.Worksheets.Count;
xlSheet.Copy(oMissing, xl.Worksheets[last]);
xl.Worksheets[last+1].name = id.ToString(); //rename to corresponding asset ID
xlSheet = xlBook.Worksheets.get_Item(last + 1);
xlSheet.Cells[2, 1] = lptp.laptop_ID;
xlSheet.Cells[2, 2] = lptp.pc_name;
xlSheet.Cells[2, 3] = lptp.manufacturer;
xlSheet.Cells[2, 4] = lptp.model;
xlSheet.Cells[2, 5] = lptp.location;
subLog.LogMessageToFile("Saving file: " + file);
xlBook.Save();
}
catch (Exception ex)
{
for (loop = 0; loop < MAX_TRIES; loop++) //// << loop -> "Unreachable code detected"
{
MessageBox.Show("File Locked - Waiting for access...");
Thread.Sleep(3000);
goto PointOfRetry;
}
subLog.LogMessageToFile("Create Error - Source: " + ex.Source);
subLog.LogMessageToFile("Create Error Message: " + ex.Message);
MessageBox.Show("Create Error: " + ex.Message, "Could not create sheet", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
subLog.LogMessageToFile("Closing session: " + file);
xl.Application.Workbooks.Close();
xl.Workbooks.Close();
xl.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xl);
}
}
MAX_TRIES and loop are both private variables inside this class.
Haven't caught an exception with this code yet, so I'm not sure if it's working or not. Any thoughts?