how to generate an asynchronous timer?

1

I need to control user sessions, for this reason I have thought about the login to ask if there is any session in the database related to the user that is being achieved.

If an active session exists, you must ask if you want to change it (if you change or your session is not active, continue with the following)

If the user wants to change the session, the following is done:

  • Execute a random that generates a random number and pass it to the main window.

  • Then a timer is generated that must be executed every 2 minutes repetitively and when it reaches the end, it must perform a select to the database with a randomly generated number through a Random (already generate the number) and check if the number is still the same or different.

  • If the number is the same, nothing should be done

  • If the number is different, the application must be closed (windows form).

I have the idea, but I need programmatic guidance.

(so far I need to program the timer with the select) the timer must be asynchronous so that the application does not hang while performing the select

         private void bunifuThinButton21_Click(object sender, EventArgs e)
                {
                    try
                    {
                        borramensajeerror();
                        if (validacion())
                        {
                            erroricono.Clear();

                            if (txt_rut.Text.Equals("") || txt_contraseña.Text.Equals(""))
                            {
                                MessageBox.Show("ingresar todos los campos");
                            }
                            else
                            {
                                if (!validarRut(txt_rut.Text))
                                {
                                    System.Windows.Forms.MessageBox.Show("usuario o contraseña incorrecto");
                                    txt_rut.Focus();
                                }
                                else
                                {

                                    logear(this.txt_rut.Text, this.txt_contraseña.Text);
                                }


                            }
                        }
                    }
                    catch (Exception f)
                    {

                        MessageBox.Show(f.Message + f.StackTrace, "errror tipo 11");
                    }
                }

     public void logear(string rut, string pass)
            {
                try {




                    //con.Open();
                    MySqlCommand cmd = new MySqlCommand("select run_usuario, id_tipousuario, id_estado, id_session, password from tb_usuarios_adm where run_usuario = '"+rut+"' AND password= '"+pass+"'", conexion.obtenerConexion());
                    MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);

                    // si lee
                    if(dt.Rows.Count == 1){

                        //identifica el tipo
                        if(dt.Rows[0][1].ToString() == "1")
                        {

                            //identifica el estado
                            if (dt.Rows[0][2].ToString() == "1")
                            {
                                //identifica la session
                                if(dt.Rows[0][3].ToString() == "1") {
                                    //this.Hide();
                                    //new administrador.Principal_administrador(dt.Rows[0][0].ToString()).Show();

                                    if (dt.Rows[0][4].ToString() == "excsa")
                                    {
                                        new cambio_contraseña_inicio(dt.Rows[0][0].ToString()).ShowDialog();
                                    }
                                    else
                                    {
                                        this.Hide();
                                        new administrador.Principal_administrador(dt.Rows[0][0].ToString()).ShowDialog();
                                    }

                                }else{

                                    DialogResult result = MessageBox.Show("desea cambiar de session", "usuario activo", MessageBoxButtons.YesNoCancel);
                                    if (result == DialogResult.Yes)
                                    {

                                    }
                                    else if (result == DialogResult.No)
                                    {

                                        MessageBox.Show("el otro usuario continua activo");

                                    }
                                    else if (result == DialogResult.Cancel)
                                    {

                                    }
                                }

                            }
                            else {
                                MessageBox.Show("estado bloqueado");
                            }


                            //identifica el tipo
                        }else if (dt.Rows[0][1].ToString() == "2")
                        {

                            //identifica el estado
                            if (dt.Rows[0][2].ToString() == "1")
                            {

                                //identifica la session
                                if(dt.Rows[0][3].ToString() == "1") {

                                    Random rdn = new Random();
                                    int a = rdn.Next(1000,9000);

                                    loginprocedimiento procedimiento = new loginprocedimiento();
                                    procedimiento.session(txt_rut.Text, a);


                                    this.Hide();
                                    new operaciones.principal_operaciones(dt.Rows[0][0].ToString(), a.ToString()).Show();


                            if (dt.Rows[0][4].ToString() == "excsa")
                            {
                                new cambio_contraseña_inicio(dt.Rows[0][0].ToString()).Show();
                            }

                                }
                                else
                                {
                                    DialogResult result = MessageBox.Show("desea cambiar de session", "usuario activo", MessageBoxButtons.YesNoCancel);
                                    if (result == DialogResult.Yes)
                                    {
                                    //se genera el random para obtener el numero aleatorio
                                        Random rdn = new Random();
                                        int a = rdn.Next(1000, 9000);
                                     // atraves de procedimiento actualizo el campo session del usuario en la base de datos
                                        loginprocedimiento procedimiento = new loginprocedimiento();
                                        procedimiento.session(txt_rut.Text, a);

                                        this.Hide();
// paso al siguiente formulario el rut y la variable generada
                                        new 
operaciones.principal_operaciones(dt.Rows[0][0].ToString(), a.ToString()).Show();

                                    }
                                    else if (result == DialogResult.No)
                                    {

                                        MessageBox.Show("la otra session continua activa");

                                    }else if (result == DialogResult.Cancel){

                                    }


                                }
                            }else{
                                MessageBox.Show("estado bloqueado");
                            }
                        }

                    }else{
                        MessageBox.Show("usuario y/o contraseña incorrecto");

                    }
                     }
                catch (Exception f) { 
                    //MessageBox.Show("ingrese caracteres validos");
                }finally{
                    //con.Close();

                    conexion.obtenerConexion().Close();
                }
            }
    
asked by sebastian bizama inostroza 20.11.2017 в 18:34
source

1 answer

1

Apart from SignalR I recommend entering the Reactive Extensions, it allows you to manage asynchronous operations in a better way:

// en algún lugar tienes un método que realiza la búsqueda de forma asíncrona 
private async Task<bool> RandomExiste(int randomVal) 
{
    var found = false;
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        // abres la conexión 
        await connection.OpenAsync();     
        // haces el query     
        await reader.ReadAsync();
        // obtienes el resultado
        found = ... 
    }
    // bla bla bla
   return found;
} 


// ya en la parte donde vas a crear el "timer"   
Observable  // Utilizamos el objeto Observable
    .Interval(TimeSpan.FromMinutes(2))    // utilizamos interval para que emita cada dos minutos
    .Select(_ => a) // obtenemos el valor random que generaste
    .SelectMany(async randomABuscar => await claseConMetodos.RandomExiste(randomABuscar)) // ejecutamos la query con el valor obtenuido en el select anterior y nos regresa true o false
    .Where(resultado => !resultado) // filtramos que solo procese los resultados falsos (del resultado del selectmany)
    .Subscribe(_ => this.Close()); // cerramos la ventana al terminar

classConMetodos is your class where you have your methods for raw operations, and RandomExiste performs a search by passing the random number as a parameter, and returns true or false.

You add the nuget package called "System. Reactive", and where you are going to use the Observables you only need to add:

using System.Observables;
using System.Observables.Linq;
    
answered by 21.11.2017 в 06:22