MysteryGuest
Registered Member
- Mar 7, 2013
- 64
- 18
Hello, here you have a small example for a multiThreaded MySQL Connection.
So make a class called ; ConnectionHandler. Let's start by declaring some thing.
Let's move on with adding the constructor!
Let's add the function Initialize()!
For this we also gonna hook our class to our FROM! (FrmMain)
Now go back to your Main form and go to the code cave and add this to the constructor!
Once that's done, we can continue working on the functions in our class. let's make the first thread! and the function!
The second thread!
and the last 1!
Almost forgot to say you also need to add the MySQL Connector, you can google this just install and add the reference once you did that Use this.
Now to get everything working add a button on your mainform and add this line!
Almost forgot to say, because our class cant interact with private, we need to change all controls that we are using to PUBLIC. you can do this by clicking the control and go to the properties bar and set private to public!
So make a class called ; ConnectionHandler. Let's start by declaring some thing.
Code:
#region declare
public MySqlConnection Con;
private string _server;
private string _database;
private string _username;
private string _password;
public FrmMain Form;
#region Threads
public bool StopOpenConThread = true;
public bool StopCloseConThread = true;
public bool StopCheckConThread = true;
#endregion
#endregion
#endregion
Let's move on with adding the constructor!
Code:
/// <summary>
/// Constructor.
/// </summary>
public ConnectionHandler()
{
Initialize();
}
Let's add the function Initialize()!
Code:
/// <summary>
/// Set connection.
/// </summary>
private void Initialize()
{
_server = "IP";
_database = "DATABASE_NAME";
_username = "DATABASE_USERNAME";
_password = "DATABASE_PASSWORD";
var connectionString = "SERVER=" + _server + ";" +
"DATABASE=" + _database + ";" +
"UID=" + _username + ";" +
"PASSWORD=" + _password + ";";
Con = new MySqlConnection(connectionString);
}
For this we also gonna hook our class to our FROM! (FrmMain)
Code:
/// <summary>
/// Hooks the main form.
/// </summary>
/// <param name="mainForm"></param>
public void HookMainForm(FrmMain mainForm)
{
Form = mainForm;
}
Now go back to your Main form and go to the code cave and add this to the constructor!
Code:
//Declaring the class.
readonly ConnectionHandler _conHandler = new ConnectionHandler();
public FrmMain()
{
_conHandler.HookMainForm(this);
}
Once that's done, we can continue working on the functions in our class. let's make the first thread! and the function!
Code:
/// <summary>
/// Starts the thread.
/// </summary>
public void StartCheckConnectionState()
{
var thread = new Thread(CheckConnectionState);
thread.Start();
}
public string ErrorHandler;
/// <summary>
/// Checks connectionstate.
/// </summary>
void CheckConnectionState()
{
while (StopCheckConThread == false)
{
try
{
switch (Con.State)
{
case System.Data.ConnectionState.Open:
Form.Invoke((MethodInvoker) (() => Form.lblSatus.ForeColor = Color.DarkGreen));
Form.lblSatus.Text = @"CONNECTED";
break;
case System.Data.ConnectionState.Closed:
Form.Invoke((MethodInvoker) (() => Form.lblSatus.ForeColor = Color.Maroon));
Form.lblSatus.Text = @"DISCONNECTED";
StopOpenConThread = false;
OpenConThread();
break;
case System.Data.ConnectionState.Connecting:
Form.Invoke((MethodInvoker) (() => Form.lblSatus.ForeColor = Color.GhostWhite));
Form.lblSatus.Text = @"CONNECTING...";
break;
case System.Data.ConnectionState.Executing:
Form.Invoke((MethodInvoker) (() => Form.lblSatus.ForeColor = Color.Goldenrod));
Form.lblSatus.Text = @"EXECUTTING...";
break;
}
}
catch (Exception ex)
{
ErrorHandler = ex.Message;
}
Thread.Sleep(50);
}
}
The second thread!
Code:
/// <summary>
/// Starts the thread.
/// </summary>
private void OpenConThread()
{
var thread = new Thread(OpenConnection);
thread.Start();
}
/// <summary>
/// Opens connection in different thread.
/// </summary>
void OpenConnection()
{
while (StopOpenConThread == false)
{
try
{
Con.Open();
StopOpenConThread = true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based
//on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
MessageBox.Show(ex.Message);
switch (ex.Number)
{
case 0:
MessageBox.Show(@"Cannot connect to server. Contact administrator");
break;
case 1045:
MessageBox.Show(@"Invalid username/password, please try again");
break;
}
StopOpenConThread = false;
}
Thread.Sleep(500);
}
}
and the last 1!
Code:
/// <summary>
/// Starts the thread.
/// </summary>
private void CloseConThread()
{
var thread = new Thread(CloseConnection);
thread.Start();
}
/// <summary>
/// Closes the connection.
/// </summary>
void CloseConnection()
{
while (StopCloseConThread == false)
{
try
{
Con.Close();
StopCloseConThread = true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
StopCloseConThread = false;
}
Thread.Sleep(500);
}
}
Almost forgot to say you also need to add the MySQL Connector, you can google this just install and add the reference once you did that Use this.
Code:
using MySql.Data.MySqlClient;
Now to get everything working add a button on your mainform and add this line!
Code:
_conHandler.StopCheckConThread = false;
_conHandler.StartCheckConnectionState();
Almost forgot to say, because our class cant interact with private, we need to change all controls that we are using to PUBLIC. you can do this by clicking the control and go to the properties bar and set private to public!
Last edited: