Search code examples
c#tabcontroltabpage

TabPage freezing application


I have a TabControl, with a few TabPages. What I am trying to do is change tabs, and have it call a richtextbox on a different tab. I will try to word the question better after code...

    void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        switch(tabControl1.SelectedTab.Name)
        {
            case "tabPage_ProdOrders":
                dostuff();
                break;
        }


        //throw new System.NotImplementedException();
    }

    void dostuff()
    {
        while (tabControl1.SelectedTab == tabControl1.TabPages["tabPage_ProdOrders"])
        {
            safeCall("testing...\n");
            Thread.Sleep(10000);
        }
    }

    delegate void SetTextCall(string s);

    public void safeCall(string s)
    {
        if (this.richTextBox1.InvokeRequired)
        {
            SetTextCall d = new SetTextCall(safeCall);
            this.Invoke(d, new object[] { s });
        }
        else this.richTextBox1.AppendText(string.Format(":{0}:\n",s));
    }

I know it looks sloppy- sorry. So the richtextbox resides on tabpage "tabPage_DEV". What I am trying to have happen is when I change to tabpage "tabPage_ProdOrders" have it append text to the richtextbox that is on "tabPage_DEV". This is moreover a test to make sure everything runs smooth, what I eventually will have happening is once "tabPage_ProdOrders" is selected I will have a gridview bound to my database, and so long as that page is selected, it will refresh from the database every X seconds. The problem is that as soon as I select the "tabPage_ProdOrders" the whole app freezes. I am assuming this is due to the Thread.Sleep() that I have within the while loop (my assumption is that since that void is part of the windows from that when it is .Sleep() the whole form is .Sleep() correct? Any guidance on a work around would be stellar.


Solution

  • I used similar to TaW's suggestions...

        delegate void populateProdOrders(DataTable dt);
    
        public void safePopulate(DataTable dt)
        {
            if (this.dataGridView2.InvokeRequired)
            {
                populateProdOrders d = new populateProdOrders(safePopulate);
                this.Invoke(d, new object[] { dt });
            }
            else this.dataGridView2.DataSource = dt;
        }
    
        public Form1()
        {
            // stuff
            System.Timers.Timer refreshProdOrders = new System.Timers.Timer(5000);
            refreshProdOrders.Elapsed += refreshProdOrders_Elapsed;
            refreshProdOrders.AutoReset = true;
            refreshProdOrders.Enabled = true;
    
        }
    
        void refreshProdOrders_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //dostuff();
            var globes = SharedResourceVariables.globals;
            DataTable dt = new DataTable();
    
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(globes.SQLConn);
            using (conn)
            {
                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("development_usp_viewProdductionOrders", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                using (cmd)
                {
                    conn.Open();
                    System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
                    dt.Load(dr);
                    conn.Close();
                }
            }
            safePopulate(dt);
        }