我更新了HttpClient异步方法中的值,但它没有,如何使用c#?

我更新了HttpClient异步方法中的值,但它没有,如何使用c#?,c#,asynchronous,httpclient,C#,Asynchronous,Httpclient,注意:\u这个问题与 调用异步方法未设置成员变量 最初,我有一个taskAssignedToUserId=1,之后我在HttpClient异步方法中更新taskAssignedToUserId,但当我退出方法时,我不会得到taskAssignedToUserId的更新值 代码 string taskAssignedToUserId="1";//default value public async static void PostRequestUserId(string url, string

注意:\u这个问题与

调用异步方法未设置成员变量

最初,我有一个taskAssignedToUserId=1,之后我在HttpClient异步方法中更新taskAssignedToUserId,但当我退出方法时,我不会得到taskAssignedToUserId的更新值

代码

string taskAssignedToUserId="1";//default value  
public async static void PostRequestUserId(string url, string api_key, string device_token, string device_type, string username)
{  
    IEnumerable<KeyValuePair<string, string>> queries = new List<KeyValuePair<string, string>>()
    {  
        new KeyValuePair<string, string>("username",username),  
        new KeyValuePair<string, string>("api_key",api_key),  
        new KeyValuePair<string, string>("device_token",device_token),  
        new KeyValuePair<string, string>("device_type",device_type)  
    };  
    HttpContent q = new FormUrlEncodedContent(queries);  
    using (HttpClient client = new HttpClient())
    {  
        using (HttpResponseMessage response = await client.PostAsync(url, q))
        {  
            using (HttpContent content = response.Content)
            {  
                var mycontent = await content.ReadAsStringAsync();  
                var jo = JObject.Parse(mycontent);  
                string validApi = jo["Result"]["valid_api"].ToString();  
                try  
                {  
                    if (validApi == "True")  
                    {  
                        taskAssignedToUserId = jo["Result"]["user_id"].ToString();  
                    }  
                    else  
                    {  
                        MessageBox.Show("API is invalid!");  
                    }  
                }  
                catch (Exception ex)  
                {  
                    MessageBox.Show(ex.Message);  
                }  
            }  
        }  
    }  
}  

//function call
MessageBox.Show(taskAssignedToUserId); // updated value  
string tasksassignedtouserid=“1”//默认值
公共异步静态void PostRequestUserId(字符串url、字符串api\u密钥、字符串设备\u令牌、字符串设备\u类型、字符串用户名)
{  
IEnumerable查询=新列表()
{  
新的KeyValuePair(“用户名”,用户名),
新的KeyValuePair(“api_密钥”,api_密钥),
新的KeyValuePair(“设备令牌”,设备令牌),
新的KeyValuePair(“设备类型”,设备类型)
};  
HttpContent q=新的FormUrlEncodedContent(查询);
使用(HttpClient=new HttpClient())
{  
使用(httpresponsemessageresponse=wait client.PostAsync(url,q))
{  
使用(HttpContent=response.content)
{  
var mycontent=await content.ReadAsStringAsync();
var jo=JObject.Parse(mycontent);
字符串validApi=jo[“Result”][“valid_api”]。ToString();
尝试
{  
if(validap==“True”)
{  
taskAssignedToUserId=jo[“结果”][“用户id”]。ToString();
}  
其他的
{  
Show(“API无效!”);
}  
}  
捕获(例外情况除外)
{  
MessageBox.Show(例如Message);
}  
}  
}  
}  
}  
//函数调用
MessageBox.Show(taskAssignedToUserId);//更新值

您应该这样修改代码:

public async static Task<int> PostRequestUserId(string url, string api_key, string device_token, string device_type, string username)
{
    //your code
    return id; //insted of setting class variable
}

您可以演示如何调用该方法。为什么要设置变量而不是让
PostRequestUserId
返回值?@stuartd
PostRequestUserId(Constants.basePath,Constants.apiKey,Constants.deviceToken,Constants.deviceType,“Steve”)可能重复的您可能需要等待呼叫谢谢,错过了。很抱歉延迟回复,因为我是stackOverflow的新手,不知道如何有效地使用它。我很重视你的所有答案/评论我在同一个类中调用该方法,当我使用wait时,它说wait只能在异步方法中使用,下面是语句
var newId=wait PostRequestUserId(Constants.basePath,Constants.apiKey,Constants.deviceToken,Constants.deviceType,“Steve”)问题已解决我在
void
函数中调用
PostRequestUserId
。我只是把
async
放在
void
之前,问题就解决了。我真的很感激你们大家
var newId = await MyStaticClass.PostRequestUserId(input_values...);
MessageBox.Show(newId);