Operation Async how to know when completed?

Asked

Viewed 85 times

0

I have 2 methods Async what to make a Insert in a cloud database..

For internet reasons, there may be transaction failure to treat this problem, I am using Commit and RollBack, to ai blz.

However, I need to execute a 3 method, but this can only be executed if these two methods Async have been successfully completed.

Could make a loop and keep testing a return condition, but I don’t think that’s very correct, and I know there’s a method called CallBack.

But how to implement the CallBack?

Down with my methods Async:

  class Upload_RaWeb
{
    Conexao_bd consql = new Conexao_bd();

    async public void Details_Upload(int id, string cod_item_Crm, int cod_cli, string razao_social, string data_rec, string data_anal, string n_Crm, string n_Nf
                            , string OF_anterior, string modelo, string material, string arranjo, string diam, string tipo, string inicio_op
                            , string fim_op, string motivo, string tag, string fabric_bomba, string mod_bomba, string prod_fluido, string temp_fluido
                            , string rpm, string api, string pres_suc, string pres_desc, string diag_falha, string analise, string conclusao
                            , string recomend, int nivel)
    {
        consql.bd_string_nuvem();

        SqlConnection sqlconn = new SqlConnection(consql.sqlconn);

        try
        {
            consql._sql = @"INSERT INTO[dbo].[DetailsRaWeb]
                                       ([id]
                                       ,[cod_item_Crm]
                                       ,[cod_cli]
                                       ,[razao_social]
                                       ,[data_rec]
                                       ,[data_anal]
                                       ,[n_Crm]
                                       ,[n_Nf]
                                       ,[OF_anterior]
                                       ,[modelo]
                                       ,[material]
                                       ,[arranjo]
                                       ,[diam]
                                       ,[tipo]
                                       ,[inicio_op]
                                       ,[fim_op]
                                       ,[motivo]
                                       ,[tag]
                                       ,[fabric_bomba]
                                       ,[mod_bomba]
                                       ,[prod_fluido]
                                       ,[temp_fluido]
                                       ,[rpm]
                                       ,[api]
                                       ,[pres_suc]
                                       ,[pres_desc]
                                       ,[diag_falha]
                                       ,[analise]
                                       ,[conclusao]
                                       ,[recomend]
                                       ,[nivel])
                                 VALUES
                                       (@id
                                       ,@cod_item_Crm
                                       ,@cod_cli
                                       ,@razao_social
                                       ,@data_rec
                                       ,@data_anal
                                       ,@n_Crm
                                       ,@n_Nf
                                       ,@OF_anterior
                                       ,@modelo
                                       ,@material
                                       ,@arranjo
                                       ,@diam
                                       ,@tipo
                                       ,@inicio_op
                                       ,@fim_op
                                       ,@motivo
                                       ,@tag
                                       ,@fabric_bomba
                                       ,@mod_bomba
                                       ,@prod_fluido
                                       ,@temp_fluido
                                       ,@rpm
                                       ,@api
                                       ,@pres_suc
                                       ,@pres_desc
                                       ,@diag_falha
                                       ,@analise
                                       ,@conclusao
                                       ,@recomend
                                       ,@nivel)";

            SqlCommand cmd = new SqlCommand(consql._sql, sqlconn);

            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            cmd.Parameters.Add("@cod_item_Crm", SqlDbType.VarChar).Value = cod_item_Crm;
            cmd.Parameters.Add("@cod_cli", SqlDbType.Int).Value = cod_cli;
            cmd.Parameters.Add("@razao_social", SqlDbType.VarChar).Value = razao_social;
            cmd.Parameters.Add("@data_rec", SqlDbType.VarChar).Value = data_rec;
            cmd.Parameters.Add("@data_anal", SqlDbType.VarChar).Value = data_anal;
            cmd.Parameters.Add("@n_Crm", SqlDbType.VarChar).Value = n_Crm;
            cmd.Parameters.Add("@n_Nf", SqlDbType.VarChar).Value = n_Nf;
            cmd.Parameters.Add("@OF_anterior", SqlDbType.VarChar).Value = OF_anterior;
            cmd.Parameters.Add("@modelo", SqlDbType.VarChar).Value = modelo;
            cmd.Parameters.Add("@material", SqlDbType.VarChar).Value = material;
            cmd.Parameters.Add("@arranjo", SqlDbType.VarChar).Value = arranjo;
            cmd.Parameters.Add("@diam", SqlDbType.VarChar).Value = diam;
            cmd.Parameters.Add("@tipo", SqlDbType.VarChar).Value = tipo;
            cmd.Parameters.Add("@inicio_op", SqlDbType.VarChar).Value = inicio_op;
            cmd.Parameters.Add("@fim_op", SqlDbType.VarChar).Value = fim_op;
            cmd.Parameters.Add("@motivo", SqlDbType.VarChar).Value = motivo;
            cmd.Parameters.Add("@tag", SqlDbType.VarChar).Value = tag;
            cmd.Parameters.Add("@fabric_bomba", SqlDbType.VarChar).Value = fabric_bomba;
            cmd.Parameters.Add("@mod_bomba", SqlDbType.VarChar).Value = mod_bomba;
            cmd.Parameters.Add("@prod_fluido", SqlDbType.VarChar).Value = prod_fluido;
            cmd.Parameters.Add("@temp_fluido", SqlDbType.VarChar).Value = temp_fluido;
            cmd.Parameters.Add("@rpm", SqlDbType.VarChar).Value = rpm;
            cmd.Parameters.Add("@api", SqlDbType.VarChar).Value = api;
            cmd.Parameters.Add("@pres_suc", SqlDbType.VarChar).Value = pres_suc;
            cmd.Parameters.Add("@pres_desc", SqlDbType.VarChar).Value = pres_desc;
            cmd.Parameters.Add("@diag_falha", SqlDbType.VarChar).Value = diag_falha;
            cmd.Parameters.Add("@analise", SqlDbType.VarChar).Value = analise;
            cmd.Parameters.Add("@conclusao", SqlDbType.VarChar).Value = conclusao;
            cmd.Parameters.Add("@recomend", SqlDbType.VarChar).Value = recomend;
            cmd.Parameters.Add("@nivel", SqlDbType.VarChar).Value = nivel;

            await sqlconn.OpenAsync();

            await cmd.ExecuteNonQueryAsync();
        }
        catch (Exception error)
        {
            MessageBox.Show("erro" + error);
        }
        finally
        {
            sqlconn.Close();
        }
    }

    async public void Foto_Upload(DataTableReader dtr)
    {
        consql.bd_string_nuvem();

        SqlConnection sqlconn = new SqlConnection(consql.sqlconn);

        await sqlconn.OpenAsync();

        SqlTransaction tran = sqlconn.BeginTransaction();

        try
        {
            consql._sql = @"INSERT INTO [dbo].[FotosRaWeb]
                                            ([id]
                                            ,[Bfoto]
                                            ,[item]
                                            ,[foto_seq]
                                            ,[zoom]
                                            ,[descr_foto])
                                        VALUES
                                            (@id
                                            ,@Bfoto
                                            ,@item
                                            ,@foto_seq
                                            ,@zoom
                                            ,@descr_foto)";              

            while (await dtr.ReadAsync())
            {
                SqlCommand cmd = new SqlCommand(consql._sql, sqlconn, tran);

                cmd.Parameters.Add("@id", SqlDbType.Int).Value = (int)dtr["id"];
                cmd.Parameters.Add("@Bfoto", SqlDbType.VarBinary).Value = (byte[])dtr["Bfoto"];
                cmd.Parameters.Add("@item", SqlDbType.VarChar).Value = dtr["item"].ToString();
                cmd.Parameters.Add("@foto_seq", SqlDbType.VarChar).Value = dtr["pic"].ToString();
                cmd.Parameters.Add("@zoom", SqlDbType.VarChar).Value = dtr["zoom"].ToString();
                cmd.Parameters.Add("@descr_foto", SqlDbType.VarChar).Value = dtr["descricao"].ToString();

                await cmd.ExecuteNonQueryAsync();
            } 
        }
        catch (Exception error)
        {
            tran.Rollback();
            MessageBox.Show("erro" + error);
        }
        finally
        {
            tran.Commit();
            sqlconn.Close();
        }
    }
}

1 answer

1


Utilize Task and await in its asynchronous methods.

You need to add the following reference at the top of your class (using):

using System.Threading.Tasks;

I modified one of your methods as an example:

public async Task<bool> Foto_Upload(DataTableReader dtr)
{
    consql.bd_string_nuvem();

    SqlConnection sqlconn = new SqlConnection(consql.sqlconn);

    await sqlconn.OpenAsync();

    SqlTransaction tran = sqlconn.BeginTransaction();

    try
    {
        consql._sql = @"INSERT INTO [dbo].[FotosRaWeb]
                                        ([id]
                                        ,[Bfoto]
                                        ,[item]
                                        ,[foto_seq]
                                        ,[zoom]
                                        ,[descr_foto])
                                    VALUES
                                        (@id
                                        ,@Bfoto
                                        ,@item
                                        ,@foto_seq
                                        ,@zoom
                                        ,@descr_foto)";              

        while (await dtr.ReadAsync())
        {
            SqlCommand cmd = new SqlCommand(consql._sql, sqlconn, tran);

            cmd.Parameters.Add("@id", SqlDbType.Int).Value = (int)dtr["id"];
            cmd.Parameters.Add("@Bfoto", SqlDbType.VarBinary).Value = (byte[])dtr["Bfoto"];
            cmd.Parameters.Add("@item", SqlDbType.VarChar).Value = dtr["item"].ToString();
            cmd.Parameters.Add("@foto_seq", SqlDbType.VarChar).Value = dtr["pic"].ToString();
            cmd.Parameters.Add("@zoom", SqlDbType.VarChar).Value = dtr["zoom"].ToString();
            cmd.Parameters.Add("@descr_foto", SqlDbType.VarChar).Value = dtr["descricao"].ToString();

            await cmd.ExecuteNonQueryAsync();
        } 

        return true; // se executar o comando acima sem erros, podemos afirmar que tudo deu certo, então retornamos TRUE
    }
    catch (Exception error)
    {
        return false; // com exceção, retornamos FALSE
    }
    finally
    {
        tran.Commit();
        sqlconn.Close();
    }
}

Added Task<bool> in his asynchronous method, we were able to verify if he executed it the way you wanted, through the return of the method. You can put any kind of return on your method. I put bool because it is easier to understand in this example.

Once this is done, we use the command await, to wait for the asynchronous method to run and return, in your case, true or false. With this, you can wait for the method and check if it had the expected behavior.

if(await FotoUpload(variavelDataTableReader))
{
   // caso o upload da foto tenha retornado true...
}
else 
{
   // caso tenha retornado false
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.