Click here to Skip to main content
15,914,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using threading method in generating the sql command
to prevent my main application getting freeze
but when i try to add a new panel after sucessfully connected with the server
but i triggered an error :
cross-thread operation not valid..

i read several article from google a lot of people said that it is not a best way to add any user control with using threading

so i tried another method
1st abort my thread as u can see at openConnection, LoadConnection.Abort(); is commend.
after i abort it i tried this method
if (loadConnection.IsAlive) {  }
            else{
                
                if (connectionStatus == "connectionSucceed") { }
                else if (connectionStatus == "connectionFail") {
                    var panel = new Panel(){ new size(100,100), location (0,0);}
                }
            }


but this time nothing happen.

is there any way to add any user control to the main form while running the sql command without freezing the UI??

What I have tried:

using System.Threading;
using MySql.Data.MySqlClient;

namespace databaseCommand{
    public static class mainCommand {
        private static Thread loadConnection = new Thread(new ThreadStart(openConnection));
        private static MySqlConnection con;
        private static string connectionStatus;

        public static void startConnection() {
            loadConnection.Start();
        }

        private static void openConnection() {
            con = new MySqlConnection("server=localhost;user=root;database=bookbardatabase;port=3306;password=");
            try{
                con.Open();
                //connectionStatus = "connectionSucceed";
                //conStatus = "Connected to server";
            }
            catch (Exception ex){
                //connectionStatus = "connectionFail";
                //conStatus = ex.ToString();
                var panel = new Panel(){ new size(100,100), location (0,0);}
            }
            //loadConnection.Abort();
        }
    }
}
Posted
Updated 7-Nov-17 14:52pm

1 solution

You cant do any UI stuff, including making a new Panel and adding it to the Form, from any other thread other the the UI (startup) thread.

You method is doing more than one thing and that's bad. Your code is doing database work and then you're trying to throw UI stuff into the mix too. Don't. This code should either return whatever your retrieving from the database or an exception. That's all, nothing else.

It's up to the calling code to figure out what to do with the result, either display it, show an error message, or whatever.
 
Share this answer
 
Comments
newbie1992 7-Nov-17 20:56pm    
ok noted sir.
so in this case
is this mean that i should just let my thread retrieve the result of database
after done that process the i continue with my UI Stuff?
Dave Kreskowiak 7-Nov-17 21:23pm    
Yes
newbie1992 7-Nov-17 21:53pm    
thanks dave, i finally solve this problem.. thank you for ur advice

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900