Saturday, December 18, 2021

await and async in asp.net C# (read txt file in asp.net) and get stored procedure

 Asynchronous programming is very popular 

When we are dealing with UI, and on button click, we use a long-running method like reading a large file or something else which will take a long time, in that case, the entire application must wait to complete the whole task. In other words, if any process is blocked in a synchronous application, the whole application gets blocked, and our application stops responding until the whole task completes.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.IO;

using System.Text;

using System.Threading.Tasks;

using RDotNet;

using RDotNet.NativeLibrary;

using RDotNet.Internals;

using RDotNetInstanceHelper;


public partial class Grid : Page

{


    protected async void Page_Load(object sender, EventArgs e)

    {


        //Task task = new Task(CallMethod);

        Task<int> task = ReadFile(@"F:\\connection string for ODK.txt");

        int length = await task;

        

        Response.Write(length);

    }


  

    static async Task<int> ReadFile(string file)

    {

        int length = 0;


        Console.WriteLine(" File reading is stating");

        using (StreamReader reader = new StreamReader(file))

        {

            // Reads all characters from the current position to the end of the stream asynchronously

            // and returns them as one string.

            string s = await reader.ReadToEndAsync();


            length = s.Length;

        }

        

        return length;

    }

}

and


get stored procedure

reference:- https://www.webtrainingroom.com/aspnetmvc/async-task

reference:- https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/async

https://www.c-sharpcorner.com/article/async-and-await-in-c-sharp/

No comments:

Post a Comment