Operation Async how to know when completed?

0

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

For reasons of the internet, the transaction may fail to handle this problem, I am using Commit and RollBack , even ai blz.

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

I could make a loop and be testing a return condition, but I do not think that's very correct, and I know there is a method called CallBack .

But how to implement CallBack ?

Below 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();
        }
    }
}
    
asked by anonymous 20.07.2017 / 21:22

1 answer

1
___ erkimt ___ Operation Async how to know when completed? ______ qstntxt ___

I have 2 methods Task to make a await in a database in the cloud ..

For reasons of the internet, the transaction may fail to handle this problem, I am using Task<bool> and bool , even ai blz.

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

I could make a true and be testing a return condition, but I do not think that's very correct, and I know there is a method called false .

But how to implement %code% ?

Below my methods %code% :

using System.Threading.Tasks;
    
______ ___ azszpr222004

Use %code% and %code% in your asynchronous methods.

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

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();
    }
}

I modified one of your methods as an example:

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

Added %code% in its asynchronous method, we verify that he performed the way you wanted, through the return of the method. You can put any type of return in your method. I put %code% because it's easier to understand in this example.

Then, use the command %code% , to wait for the asynchronous method execute and return, in your case, %code% %% or %code% . With this, you can wait for the method and see if it has the expected behavior.

using System.Threading.Tasks;
    
___
20.07.2017 / 22:02