Ejemplo n.º 1
0
 static async Task DoTaskAsync()
 {
     var connStr = GetMySqlConnString();
     var conn = new MySqlConnection(connStr);
     conn.UseConnectionPool = true;
     await conn.OpenAsync();
     await conn.CloseAsync();
 }
        public static async void T_InsertAndSelect_TAP()
        {

            System.Diagnostics.Stopwatch stopW = new System.Diagnostics.Stopwatch();
            stopW.Start();
            await Task.Run(async () =>
            {
                var connStr = GetMySqlConnString();
                var conn = new MySqlConnection(connStr);
                conn.UseConnectionPool = true;
                await conn.OpenAsync();
                //------------------------------------------
                //drop table if exist
                {
                    string sql = "drop table if exists test001";
                    var cmd = new MySqlCommand(sql, conn);
                    await cmd.ExecuteNonQueryAsync();
                }
                //------------------------------------------ 
                {
                    string sql = "create table test001(col_id  int(10) unsigned not null auto_increment, col1 int(10)," +
                    "col2 char(2),col3 varchar(255),col4 datetime, primary key(col_id) )";
                    var cmd = new MySqlCommand(sql, conn);
                    await cmd.ExecuteNonQueryAsync();
                }
                //---------------------------------------------
                for (int i = 0; i < 2000; ++i)
                {

                    string sql = "insert into test001(col1,col2,col3,col4) values(10,'AA','123456789','0001-01-01')";
                    var cmd = new MySqlCommand(sql, conn);
                    await cmd.ExecuteNonQueryAsync();
                }
                //--------------------------------------------
                //select back
                {
                    string sql = "select * from test001";
                    var cmd = new MySqlCommand(sql, conn);
#if DEBUG
                    conn.dbugPleaseBreak = true;
#endif
                    var reader = await cmd.ExecuteReaderAsync();
                    while (reader.Read())
                    {
                        //test immediate close
                        await reader.CloseAsync();
                    }
                    await reader.CloseAsync();
                }
            });
            //--------------------------------------------
            stopW.Stop();
            Report.WriteLine("avg:" + stopW.ElapsedTicks);
        }
Ejemplo n.º 3
0
        public static async void T_OpenAndClose_TAP()
        {

            System.Diagnostics.Stopwatch stopW = new System.Diagnostics.Stopwatch();
            stopW.Start();
            //----------------------------------------
            //1. form1: call another async method
            await DoTaskAsync();
            //--------------------------------------------
            //2. form2: create task, then start and wait
            var t2 = new Task(async () =>
            {
                var connStr = GetMySqlConnString();
                var conn = new MySqlConnection(connStr);
                conn.UseConnectionPool = true;
                await conn.OpenAsync();
                await conn.CloseAsync();
            });
            t2.Start();
            t2.Wait();
            //--------------------------------------------
            //form3: use simple helper, create task and run,store task in **named** var and  wait
            var t3 = Task.Run(async () =>
            {
                var connStr = GetMySqlConnString();
                var conn = new MySqlConnection(connStr);
                conn.UseConnectionPool = true;
                await conn.OpenAsync();
                await conn.CloseAsync();
            });
            await t3;//**
            //--------------------------------------------
            //form4: use simple helper, create task and run, store task in **anonymous** var and wait
            await Task.Run(async () =>
            {
                var connStr = GetMySqlConnString();
                var conn = new MySqlConnection(connStr);
                conn.UseConnectionPool = true;
                await conn.OpenAsync();
                await conn.CloseAsync();
            });
            //--------------------------------------------
            stopW.Stop();
            Report.WriteLine("avg:" + stopW.ElapsedTicks);

        }