question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked karenpayneoregon commented

C# Function will work if i do not use async keyword

Please see this function body and tell me does it work as asynchronously or synchronously ? Thanks

 private Task<bool> SaveData(string xml)
 {
  bool status = false;
  try
  {
  using (var con = new SqlConnection(ConnectionString))
  using (var cmd = new SqlCommand("USP_CSMAddTabTickerAndClientWise", con))
  using (var da = new SqlDataAdapter(cmd))
  {
  con.Open();
  cmd.CommandType = CommandType.StoredProcedure;
  cmd.Parameters.Add("@Xml", SqlDbType.Xml).Value = xml;
  status = (cmd.ExecuteNonQuery() > 0 ? true : false);
  }
  }
  catch (Exception ex)
  {
  status = false;
  MessageBox.Show("Error " + ex.Message);
  }
    
  return Task.FromResult(status);
 }

 string xml="sample data";
 var task1 = SaveData(xml);
 await Task.WhenAll(task1);
 if ((bool)task1.Result)
 {
    
 }

EDIT

 private async Task<bool> SaveData(string xml)
 {
     bool status = false;
     try
     {
         using (var con = new SqlConnection(ConnectionString))
         using (var cmd = new SqlCommand("USP_CSMAddTabTickerAndClientWise", con))
         using (var da = new SqlDataAdapter(cmd))
         {
             con.Open();
             cmd.CommandType = CommandType.StoredProcedure;
             cmd.Parameters.Add("@Xml", SqlDbType.Xml).Value = xml;
             status = ( await cmd.ExecuteNonQueryAsync() > 0 ? true : false);
         }
     }
     catch (Exception ex)
     {
         status = false;
         MessageBox.Show("Error " + ex.Message);
     }
    
     return status;
 }
    
 var task1 = SaveData(xml);
 await Task.WhenAll(task1);
 if ((bool)task1.Result)
 {
    
 }

Now i restructure my code. new structure code would be consider & working as asynchronously ? please guide me. thanks


dotnet-csharp
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Unsure why you use WhenAll for a single task? Also if you want async you would code very differently, Have you read the docs?


0 Votes 0 ·
TZacks-2728 avatar image TZacks-2728 karenpayneoregon ·

please see Edit section where i restructure my code. My new restructure code would be consider & working as asynchronously ?
please guide me. thanks

0 Votes 0 ·

Hi, yes your code is asynchronously but there is no need for WhenAll. If time permits, see a code sample done a while ago that might help clear things up to what I mean by no need for WhenAll,


1 Vote 1 ·
karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered TZacks-2728 commented

You would be better off with this pattern which is asynchronous and needs no WhenAll, Call it without creating a new instance of the class then the result can be deconstructed from the named value tuple for success/failure and if the exception is not null show exception.Message.

 using System;
 using System.Data;
 using System.Data.SqlClient;
 using System.Threading.Tasks;
    
 namespace Project1.Classes
 {
     public class DataOperations
     {
         private static  string ConnectionString = "TODO";
         public static async Task<(bool success, Exception exception)> SaveData(string xml)
         {
    
             try
             {
                 await using var con = new SqlConnection(ConnectionString);
                 await using var cmd = new SqlCommand("USP_CSMAddTabTickerAndClientWise", con);
                 using var da = new SqlDataAdapter(cmd);
                 await con.OpenAsync();
    
                 cmd.CommandType = CommandType.StoredProcedure;
                 cmd.Parameters.Add("@Xml", SqlDbType.Xml).Value = xml;
                 return ((await cmd.ExecuteNonQueryAsync() > 0), null);
             }
             catch (Exception ex)
             {
                 return (false, ex);
             }
         }
     }
 }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Respected Madam,

please see Edit section in my post where i restructure my code. My new restructure code would be consider & working as asynchronously ?
please guide me. thanks

0 Votes 0 ·
Paul-5034 avatar image
1 Vote"
Paul-5034 answered TZacks-2728 commented

It's a synchronous method that wraps it's result in a Task that's usually used to propagate asynchronous results.

If you change your return type to bool and return this:

return status;


Instead of this:

return Task.FromResult(status);


Then the result will be the same. Then you can change the rest of your code to:

string xml="sample data";        
var result = SaveData(xml);
if (result)
{        
    
}
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Respected Madam,

please see Edit section in my post where i restructure my code. My new restructure code would be consider & working as asynchronously ?
please guide me. thanks

0 Votes 0 ·