Async Help

newonehere69

New member
Joined
Nov 28, 2016
Messages
2
Programming Experience
Beginner
Hi,

I am new to C# and I need to know how I can pass a user information to a async method.
C#:
//User Class            
 public class User
    {       
        [JsonProperty("username")]
        public string UserName { get; set; }
        [JsonProperty("firstname")]
        public string FirstName { get; set; }
        [JsonProperty("lastname")]        
        public string LastName { get; set; }


 public async static Task<bool> Create(User user)
        {


            var byteArray = Encoding.ASCII.GetBytes($"{Username}:{Password}");
            string load = JsonConvert.SerializeObject(user);
            StringContent c = new StringContent(load, Encoding.UTF8, "application/json");
            string temp = String.Empty;
            using (HttpClient http = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                http.DefaultRequestHeaders.Add("Api-Key", ApiKey);
                HttpResponseMessage res = http.PutAsync(RESTUrl + "Users", c).Result;
                if (res.IsSuccessStatusCode)
                {


                }
                if (res.Content != null)
                {                   
                    string resContent = res.Content.ReadAsStringAsync().Result;
                    temp = resContent;
                }
                return res.IsSuccessStatusCode;
            }
        }
}     

//On Default Page Code Behind PageLoad
  string uname = HttpContext.Current.User.Identity.Name;
//Info is a separate class that gets the user information
  Info info = new Info(uname);

//I need to pass that user information (username,firstname, lastname, ect..) to the async method that is on the users class(top)
//This is what I've been trying and is where the confusion is. 
/The Create method takes a parameter of type User. It must pass a parameter of type User.
bool x = await User.Create(info);

Any ideas how I can accomplish this?

Thanks!
 
Passing data to an async method is exactly the same as passing data to a non-async method. You simply pass an argument to the parameter as normal. For instance, if you have this method:
private void DoSomething(object arg)
{
    // ...
}
and you call it like this:
DoSomething(someObject);
then, if you have this method:
private async Task DoSomething(object arg)
{
    // ...
}
then you would call it like this:
await DoSomething(someObject);
The only difference in the call is the 'await' keyword preceding the method name. With regards to that 'await', what it basically means is "wait here until this async method completes. If you just stick the 'await' before the method call then it works pretty much like any synchronous method, although there can still be some advantages. Where it can be directly advantageous in your code is if you don't need to use the result of an asynchronous method until later. In that case, you can call the method synchronously, i.e. without the 'await' keyword, and it will return a Task. You can then do other work while that Task is being executed in the background. Once you finish your work, you can then await the Task, which may already have completed or may still be working:
var myTask = DoSomething(someObject);

// Do some other work here that doesn't rely on the result of DoSomething.

await myTask;
What you can also do is call multiple asynchronous methods and then await them all, which means that they will all execute simultaneously and your code will wait until they've all completed:
var myTask = DoSomething(someObject);
var myOtherTask = DoSomethingElse(someOtherObject);

await myTask;
await myOtherTask;
 
I see now that you have put important information inside your code box, where it's well hidden, instead of outside, where it belongs. The issue has absolutely nothing to do with async methods. The problem should be obvious. You say yourself that the method expects a User object so what use is it for you to create an Info object and pass that? If the method expects a User object then create a User object and pass it to the method.
 
Thanks for replying.
When I tried to pass the user info "Info info = new Info(uname);", await Provisioning.User.CreatUser(info);" to "public async static Task<bool> Create(User user)" I get an error. "cannot convert from 'Info' to 'User'.
 
The Create method takes a parameter of type User. It must pass a parameter of type User.
This is also what jmcilhinney said. Create a User object and pass that.
 
Back
Top Bottom