0

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

  • 2
    Maybe you should implement this functionality in a Background-Service. Start here: https://learn.microsoft.com/en-us/dotnet/core/extensions/workers – TheTanic Mar 20 '23 at 09:38
  • The following may be helpful: https://stackoverflow.com/a/73806343/10024425 - it's VB.NET. See the resources listed in the post which contain some code for C#. – Tu deschizi eu inchid Mar 20 '23 at 15:45

1 Answers1

2

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

Run your application as a service. You will get some error handling & restart functionality for free. Note that services cannot have an UI, so if you want an UI you need to use some type of workaround.

However, the first thing you should do is find out what causes the problem. Start by ensuring you have a good logging, so you can track what happens. Logging libraries (like NLog) allow you to add very fine grained log messages, and decide at runtime from what part and to what details you want to log. It should also allow you to configure how long to keep logs etc.

If an unhandled exception causes the program to quit, check the event viewer, it should list program crashes. If the program just hangs, use a debugger to find out why. You can take a memory dump with Process explorer (a very useful tool in many circumstances), that can be imported in visual studio for analysis. Or you can use a stand alone debugger like dn-spy.

JonasH
  • 28,608
  • 2
  • 10
  • 23