troubleshooting Question

C# WPF how to ensure application keeps running 24/7

Avatar of Jeff Heilman
Jeff HeilmanFlag for United States of America asked on
DatabasesC#SQL* WPF
10 Comments1 Solution142 ViewsLast Modified:
I have a simple C# WPF application that shows production data in a datagrid that is bound to a tableadapter in a dataset linked to a SQL database table.  The only function is to display this information 24/7, it is not used to add or edit data.  My question is pretty general, what is the best way to ensure this application keeps running?  Every now and then it will stop working until I reopen the application.  Perhaps the way to handle it is to just relaunch the app any time it throws an exception?  The only message I get right now when it crashes is "Production Counter has stopped working.".

I'm new to C# and I am interested in your opinions/sample code/ideas, I'm looking to learn.  Thank you!

namespace ProductionCounter
{

    public partial class MainWindow : Window
    {
        public DispatcherTimer _timer = null;
        public MainWindow()
        {
            InitializeComponent();
            Initializeload();

            // Start the timer to refresh every 10000ms thereafter (change as required)
            _timer = new DispatcherTimer();
            _timer.Tick += Each_Tick;
            _timer.Interval = new TimeSpan(0, 0, 0, 0, 10000);
            _timer.Start();

            var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            lblVersion.Content = string.Format("Version {0}", version);

            string machineName = Environment.MachineName;            
            lblMachineName.Content = machineName;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Initializeload();
        }

        private void Each_Tick(object o, EventArgs sender)
        {
             Initializeload();
        }

        private void Initializeload()
        {
            
                string machineName = System.Environment.MachineName;

                string connString = ConfigurationManager.ConnectionStrings["ProductionCounter.Properties.Settings.MyConnectionString"].ConnectionString;

                SqlDataAdapter da = new SqlDataAdapter("Select Department From tblQMSTerminalID WHERE TerminalID = @mach", connString);
                da.SelectCommand.Parameters.AddWithValue("@mach", machineName);
                DataSet ds = new DataSet();
                da.Fill(ds);
                string pl = ds.Tables[0].Rows[0]["Department"].ToString();
                lblProdLine.Content = pl;

                SqlDataAdapter sa = new SqlDataAdapter("SELECT MAX(UPWHID) AS MaxOfUPWHID, Shift, Dept, MAX(ShiftDate) AS MaxShiftDate, " +
                    "MIN(StartTime) AS MinStartTime FROM tblQMSUPWH WHERE Dept = @pline GROUP BY Dept, Shift", connString);
                sa.SelectCommand.Parameters.AddWithValue("@pline", pl);
                DataSet sd = new DataSet();
                sa.Fill(sd);
                string shift = sd.Tables[0].Rows[0][1].ToString();
                string sdate = sd.Tables[0].Rows[0][3].ToString();
                lblShift.Content = shift;
                lblDate.Content = Convert.ToDateTime(sdate);

                SqlDataAdapter pa = new SqlDataAdapter("SELECT Dept, Shift, ShiftDate, FLOOR(SUM(RunningGoal)) AS Target, SUM(UnitsProd) AS Produced " +
                    "FROM vw_PRODCOUNTER_HourlyProduction WHERE Dept = @pline AND Shift = @shift AND ShiftDate = @sdate GROUP BY Dept, Shift, ShiftDate", connString);
                pa.SelectCommand.Parameters.AddWithValue("@pline", pl);
                pa.SelectCommand.Parameters.AddWithValue("@shift", shift);
                pa.SelectCommand.Parameters.AddWithValue("@sdate", sdate);
                DataSet pd = new DataSet();
                pa.Fill(pd);
                string prod = pd.Tables[0].Rows[0][4].ToString();
                string target = pd.Tables[0].Rows[0][3].ToString();
                txtProduced.Text = prod;
                txtTarget.Text = target;
          
        }
    }
}
ASKER CERTIFIED SOLUTION
Éric Moreau
Senior .Net Consultant
Join our community to see this answer!
Unlock 1 Answer and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros