Click here to Skip to main content
15,891,850 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
now i get this is a async TASK i've used basic async task before to do simple task but
what im trying to do is understanding the return type and how to implement it c# code
ive read a dozen or so articles on explaining async task and interfacers i just cant seem to wrap my head around it.

C#
Task<IGuildUser> AddGuildUserAsync(ulong userId, string accessToken, Action<AddGuildUserProperties> func = null, RequestOptions options = null)



*documentation
https://docs.stillu.cc/api/Discord.IGuild.html#Discord_IGuild_AddGuildUserAsync_System_UInt64_System_String_System_Action_Discord_AddGuildUserProperties__Discord_RequestOptions_

What I have tried:

tried discord API for help.
read and watch videos regarding async task and interface but cant wrap my head around it
Posted
Updated 18-Feb-19 2:36am

1 solution

A method with the signature:

Task<iguilduser> AddGuildUserAsync(ulong userId, string accessToken, Action<addguilduserproperties> func = null, RequestOptions options = null)


Has the return type: Task<iguilduser>, with the IGuildUser implementation wrapped inside the Task object.

What you want to obtain, I assume, is the IGuildUser, so you have 3 options:

IGuildUser user = AddGuildUserAsync(ulong userId, string accessToken, Action<addguilduserproperties> func = null, RequestOptions options = null).Result;


This will run synchronously, and is not really what async is for.

Alternatively, you 'await' the menthod, which effectively does a similar thing, but will run asynchronously, and can only be done inside another async method:

IGuildUser user = await AddGuildUserAsync(ulong userId, string accessToken, Action<addguilduserproperties> func = null, RequestOptions options = null)


The third option, if you want to run from a non-ansync method, may be to fire it up in a separate task:

IGuildUser user = Task.Run(() => AddGuildUserAsync(ulong userId, string accessToken, Action<addguilduserproperties> func = null, RequestOptions options = null).Result;
 
Share this answer
 
v2
Comments
Richard MacCutchan 18-Feb-19 9:34am    
This would be easier to read if you formatted your code blocks properly.
Wastedtalent 18-Feb-19 10:43am    
I have corrected the formatting.
Richard Deeming 18-Feb-19 9:53am    
Your third option won't work. Task.Run returns the Task<TResult>, not the result of the task. You'd still need to block or await for the result of the task, so it wouldn't give you any benefit over the other two options.
Wastedtalent 18-Feb-19 10:44am    
Thanks, I missed the .Result off.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900