I have the following problem, I have to make a program that checks if X things are active, querying a database and displaying them in a form.
It's something like this more or less.
Note: I omit some parts of the code since I think they are not necessary for the understanding of the code or the resolution of the problem.
_____________________________________
|_____________________________________|
| Algo | Estado = activo |
| Otra cosa | Estado = Pausado |
| Y algo | Estado = Suspendido |
|____ ______ ____________ ____________|
The thing is that I want to control what appears in this form, the states, from a thread that is in a different class, that is, the program will do more things apart from this and I am interested in dividing it by classes and others.
The problem arises when I have to access it from the thread, which itself is a static method. This is the idea I had, I attach an example of the two classes.
public partial class Form1 : Form {
{
public Form1(){
InitializeComponent();
}
private void controlItem_Click(object sender, EventArgs e){
//Iniciamos el hilo padre.
Thread principal = new Thread(Hilos.mainthread);
principal.Start();
}
}
class Hilos
{
static MySqlConnection conn;
private Boolean error;
public static void mainthread() {
//Creamos la conexión con la BBDD
string myConnectionString = "server=xx.xx.xx.xx;uid=xxxxx;" +
"pwd=xxxxxx*;database=xxxxxx";
//Intentamos la conexión
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = myConnectionString;
conn.Open();
data=//Aquí iría una consulta MySQL
if(data==xxxx){
//Lo que quiero hacer es Form1.textbox1.text=Activo
//Evento o algo para hacer que funcione
}else if(data==yyyy){
//Lo que quiero hacer es Form1.textbox1.text=Suspendido
//Evento o algo para hacer que funcione
}else{
//Lo que quiero hacer es Form1.textbox1.text=Pausado
//Evento o algo para hacer que funcione
}
}
}
So, once the code is exposed, what I want to know is, how can I make it so that when one of the conditions is met, an event is launched or something (I still don't know about events), in order to change the corresponding Textbox.
Although there are ways to do what you ask and how you ask, the recommended pattern is to not do any UI work in the method that does the database queries.
Rather, it's better to divide the responsibilities so that your method that works with the database simply returns a value that the consumer then takes care of assigning to a control in the UI.
And I assume that your desire to use a thread for database work is so that the UI thread is idle while the query is being performed. If this is the case, instead of working with a thread directly, today it is recommended to use a
Task
in conjunction withasync/await
.Here is an example of how to structure it:
It is also worth mentioning that, depending on the library you use to query the database, it may already include asynchronous methods. If this is the case, the
Task
.Firstly, I have added a class with a status and name attribute, to be able to identify what the process is in case I need it when debugging.
In the same Thread, I create the object
Estado xxxx = new Estado("Nombre del programa a monitorizar")
Then, in la
Class hilos
I create the state type object.Being the public object, I can use it from the Form1 class where I simply do.