C# sync function to async

c# call synchronous method asynchronously
c# call async method without await
c# await synchronous method
c# async/await
asynccontext c#
.net core run async method synchronously
c# call async method from main
c# waitandunwrapexception

How I can change this function to async with Framework 4.6.1? The DGV has a lot of data and blocks the rest of the processes.

DGV is updated every 30 seconds with timer.

any other solution do not block the next process until this is finnished?

I may have something wrong, I am starting in the world of programming and I have many things to learn.

private void FillTimes()
        {
            var strResponse = CallJson($"RESTAPI URL");
            if (strResponse != null)
            {
                var jResult = JsonConvert.DeserializeObject<JsonResults>(strResponse);

                BindingSource bsResults = new BindingSource();
                bsResults.DataSource = jResult.Results;

                if (bsResults.DataSource != null)
                {
                    DgvOnline.DataSource = bsResults.DataSource;
                }
            }
        }

CallJson

 private string CallJson(string strURL)
            {
                RestTiming rJson = new RestTiming();
                rJson.endPoint = strURL;
                rJson.token = apiToken;

                string strResponse = string.Empty;

                strResponse = rJson.makeRequest();

                return strResponse;
            }

ResTiming

using System;
using System.IO;
using System.Net;
using System.Windows.Forms;

namespace Timing
{
    public enum httpVerb
    {
        GET,
        POST,
        PUT,
        DELETE

    }
    class RestTiming
    {
        public string endPoint { get; set; }
        public string token { get; set; }
        public httpVerb httpMethod { get; set; }
        public string userName { get; set; }
        public string userPassword { get; set; }
        public string postJSON { get; set; }

        public RestTiming()
        {
            endPoint = string.Empty;
            token = string.Empty;
        }

        public string makeRequest()
        {
            if (InternetAvailable())
            {
                string strResponseValue = string.Empty;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);

                request.Method = httpMethod.ToString();
                request.ContentType = "application/json";
                request.Accept = "application/json";
                request.Headers.Add("Authorization", token);
                request.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";

                if ((request.Method == "POST" || request.Method == "PUT") && postJSON != string.Empty)
                {
                    using (StreamWriter swJSONPayload = new StreamWriter(request.GetRequestStream()))
                    {
                        swJSONPayload.Write(postJSON);
                        swJSONPayload.Close();
                    }
                }

                HttpWebResponse response = null;

                try
                {
                    response = (HttpWebResponse)request.GetResponse();

                    // Process the response stream... (could be JSON, XML or HTML etc...)

                    using (Stream responseStream = response.GetResponseStream())
                    {
                        if (responseStream != null)
                        {
                            using (StreamReader reader = new StreamReader(responseStream))
                            {
                                strResponseValue = reader.ReadToEnd();
                            }// End of StreamReader
                        }
                    }// End of using ResponseStream

                }
                catch (WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                    {
                        var resp = (HttpWebResponse)ex.Response;
                        if (resp.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            MessageBox.Show("Unauthorized", "Unauthorized", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            Environment.Exit(1);
                        }
                    }
                }

                return strResponseValue;
            }
            else
            {
                return null;
            }
        }

        private static Boolean InternetAvailable()
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    using (client.OpenRead("http://www.google.com/"))
                    {
                        return true;
                    }
                }
            }
            catch
            {
                return false;
            }
        }
    }
}

Thanks

I'm not going to weigh in on whether your code should be async or not.

If you want the synchronous work to happen in a background thread, you can execute it using Task.Run as follows:

private async Task FillTimes()
{
    return Task.Run(() => {
    {
        var strResponse = CallJson($"RESTAPI URL");
        if (strResponse != null)
        {
            var jResult = JsonConvert.DeserializeObject<JsonResults>(strResponse);

            BindingSource bsResults = new BindingSource();
            bsResults.DataSource = jResult.Results;

            if (bsResults.DataSource != null)
            {
                DgvOnline.DataSource = bsResults.DataSource;
            }
        }
    }
}

How to Call an Async Method Synchronously in C#, Running sync method as async. Any standard sync method can be run inside a Task by using the� The following are few tips and tricks for working with async in C#. This is as much a reference for myself as anyone else, but I figured it would probably useful for others as well. Run sync method as "async" Task.Run(() => DoSyncStuff()); Technically, this is fake async. It's

Update

If CallJson is IO Bound work then you could make that async, also you would append the Async suffix to the the method name to be consistent. Your resulting method would look like this.

private async Task FillTimes()
{
   var strResponse = await CallJsonAsync($"RESTAPI URL");
   if (strResponse != null)
   {
      var jResult = JsonConvert.DeserializeObject<JsonResults>(strResponse);

      BindingSource bsResults = new BindingSource();
      bsResults.DataSource = jResult.Results;

      if (bsResults.DataSource != null)
      {
         DgvOnline.DataSource = bsResults.DataSource;
      }
   }
}

And your async method would look like this

private async Task<responseType> CallJsonAsync(<something>)
{
    ... 

    await SomethingAsync(...);

    ...

    return something;
}

Original

How I can change this function to async with Framework 4.6.1

There is no obvious IO Bound work, and no method that would lend itself towards an async call (that i can see).

So to answer the question, "How I can change this function to async?", the answer is you shouldn't, let it be synchronous.

If this does not update the UI, you could call this method from a Task, and await that

AsyncUtil - C# Helper class to run async methods as sync, Learn how to call synchronous methods asynchronously in . NET Framework enables you to call any method asynchronously. or if you're using C# with Visual Studio, see Asynchronous Programming Model (APM) for a� You can represent that work with the following code: C#. static async Task<Toast> MakeToastWithButterAndJamAsync(int number) { var toast = await ToastBreadAsync (number); ApplyButter (toast); ApplyJam (toast); return toast; } The preceding method has the async modifier in its signature.

Updated

  class RestTiming
  {
    public string endPoint { get; set; }
    public string token { get; set; }
    public httpVerb httpMethod { get; set; }
    public string userName { get; set; }
    public string userPassword { get; set; }
    public string postJSON { get; set; }

    public RestTiming()
    {
        endPoint = string.Empty;
        token = string.Empty;
    }

    public async Task<string> makeRequest() //1. Changed to async and return type
    {
        if (InternetAvailable())
        {
            string strResponseValue = string.Empty;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);

            request.Method = httpMethod.ToString();
            request.ContentType = "application/json";
            request.Accept = "application/json";
            request.Headers.Add("Authorization", token);
            request.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";

            if ((request.Method == "POST" || request.Method == "PUT") && postJSON != string.Empty)
            {
                using (StreamWriter swJSONPayload = new StreamWriter(await request.GetRequestStreamAsync())) //2. changed to asynchronous call
                {
                    swJSONPayload.Write(postJSON);
                    swJSONPayload.Close();
                }
            }

            WebResponse response = null; //(a) updated

            try
            {
                response =await request.GetResponseAsync(); 

                // Process the response stream... (could be JSON, XML or HTML etc...)

                using (Stream responseStream =response.GetResponseStream()) //(b) updated
                {
                    if (responseStream != null)
                    {
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            strResponseValue = await reader.ReadToEndAsync(); // (c) updated
                        }// End of StreamReader
                    }
                }// End of using ResponseStream

            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                {
                    var resp = (HttpWebResponse)ex.Response;
                    if (resp.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        MessageBox.Show("Unauthorized", "Unauthorized", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        Environment.Exit(1);
                    }
                }
            }

            return strResponseValue;
        }
        else
        {
            return null;
        }
    }

    private static Boolean InternetAvailable()
    {
        try
        {
            using (WebClient client = new WebClient())
            {
                using (client.OpenRead("http://www.google.com/"))
                {
                    return true;
                }
            }
        }

Changes in the second method

private async Task<string> CallJson(string strURL) //1. Changed to async and return type
        {
            RestTiming rJson = new RestTiming();
            rJson.endPoint = strURL;
            rJson.token = apiToken;

            string strResponse = string.Empty;

            strResponse =await rJson.makeRequest(); //2. made asynchronous 

            return strResponse;
        }

The change in the first method

    private async Task FillTimes() // 1. Make async and change return type
    {
        var strResponse =await CallJson($"RESTAPI URL"); // 2. add await to make asynchronous
        ...
    }

Please let me know if this helps. Additionally, check this link (Getting the Response of a Asynchronous HttpWebRequest)

Task.RunSynchronously Method (System.Threading.Tasks , Now that I use c# 7.0 I hoped for a Solutions - but I do not see it. Anywhere in a library (where I have no source Code) there is a Methode in a� Obtain a WaitHandle using the IAsyncResult.AsyncWaitHandle property, use its WaitOne method to block execution until the WaitHandle is signaled, and then call EndInvoke. Poll the IAsyncResult returned by BeginInvoke to determine when the asynchronous call has completed, and then call EndInvoke. Pass a delegate for a callback method to BeginInvoke.

Calling Synchronous Methods Asynchronously, Run sync method as "async". Task.Run(() => DoSyncStuff());. Technically, this is fake async. It's still blocking, but it runs on a background thread� If your async method is dependent on other async methods, the easiest approach is to use the async keyword: private static async Task<DateTime> CountToAsync(int num = 10) { for (int i = 0; i < num; i++) { await Task.Delay(TimeSpan.FromSeconds(1)); } return DateTime.Now; } If your async method is doing CPU work, you should use Task.Run:

Synchronous call of async Method - MSDN, Using async void methods. Every now and then you'll find yourself in a synchronous method (i.e. one that doesn't return a Task or Task� A program with an async Main() method doesn't compile; at some point you've got to bridge the gap between the sync and async worlds. It's not a " very rare situation" , it's necessary in literally every program that calls an async method.

C# Async Tips & Tricks, Writing new code in async but calling sync � c# .net asynchronous-programming technical-debt. I am writing some new code and would like to write it using async � When converting from synchronous to asynchronous code, any method returning a type T becomes an async method returning Task<T>, and any method returning void becomes an async method returning Task. The following code snippet illustrates a synchronous void-returning method and its asynchronous equivalent: XML.

Comments
  • What is CallJson? what is the code in this?
  • There is nothing here looks like it needs to be async. Perhaps CallJson depending on what it is doing.
  • Asynchrony is for managing latency. What is the high-latency operation here?
  • Updated the post with CallJson code
  • Maybe duplicate of stackoverflow.com/questions/10928528/… (or any "Get JSON async in C#" posts)?
  • You should also note, wrapping/offloading CPU bound workloads in the implementation of an async method is a code smell and is called fake async, its confusing, its not scalable, it uses up valuable thread-pool threads and can have unintended consequences. Even if this is IO bound, its very suspicious and should be warned against
  • @MichaelRandall Certainly. I completely agree. Most beginners are looking to StackOverflow for simple solutions to simple problems. StackOverflow isn't supposed to be an extensive curriculum. If your detailed answer and warnings are what the OP desires, then they can mark your answer as accepted. If the OP wants a simple answer that doesn't go into the numerous flaws and gotchas of async/await/TPL then this answer is perfect.
  • @MichaelRandall Well, what's the alternative to Task.Run() for CPU-bound computation? BackgroundWorker? No thank you.
  • @Joker_vD you missed the point, you just should wrap CPU bound in a task then call it an async method, let the method be syncronions let the caller call task run. For all the reasons I stated
  • Thanks you, but I have problems with the first method that you change, with the 3 and 4 changes. 3 you can not convert the type 'System.Threading.Tasks.Task <System.Net.WebResponse>' into 'System.Net.HttpWebResponse'4 "HttpWebResponse" does not contain a definition for "GetResponseStreamAsync"
  • I updated the code with comments, please try and let me know if it works.