I am actually stuck at a point of development. I have written quite a big code in C# (have not so much experience in C#). What the code basically does is that it runs an infinite loop in which it calls another C# file or class. That file first connects to my database (Sybase) on my stores, see if the record is not in my existing database of head office, then send this data to the HO database. So the basic purpose is to get the live data from my stores. The code runs fine but what I am encountering is that it gets stuck after a period of time. I will be putting my exe on stores server and I don't want my staff to interact with the file or close the app accidently. So I put code to hide the window as well as to remove icon from taskbar.
I want to know that is there a way that my app runs continually and if ever some error occurs at any point, restart the app. The error that occurs after like hour or so is that it gives connection error
Code I am using in my program.cs
var storeid = Convert.ToInt32(ConfigurationManager.AppSettings["StoreId"].ToString());
while (true)
{
using (var con = OracleHelper.GetCon())
{
POSHD_VIEW.POSHD_table(storeid, con);
Thread.Sleep(1000);
if (DateTime.Now.Minute % 10 == 0)
{
Console.Write("New Connection");
con.Dispose();
}
}
}
Get con function in Oracle Helper
public static OracleConnection GetCon()
{
String connectionString = "*******************";
int retryCount = 0;
int maxRetries = 5;
int retryDelaySeconds = 10;
while (true)
{
try
{
OracleConnection connection = new OracleConnection(connectionString);
connection.Open();
return connection;
}
catch
{
if (retryCount < maxRetries)
{
retryCount++;
Console.WriteLine($"Failed to connect to Oracle (attempt {retryCount}/{maxRetries}). Retrying in {retryDelaySeconds} seconds...");
Thread.Sleep(retryDelaySeconds * 1000);
}
else
{
throw;
}
}
}
}
My poshd_view contains for loops, some sql queries and insertions