简体   繁体   中英

c# return data from Asynchronous function

I have two methods

public void GetEmp()
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri("http://sdw2629/empservice/EmployeeInfo.svc/Employee"));
    request.Method = "GET";
    request.ContentType = "application/json; charset=utf-8";

    request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);  

}

and

private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
    HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
    using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult))
    {
        using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
        {
            string results = httpwebStreamReader.ReadToEnd();
            //execute UI stuff on UI thread.              
        }
    }
}  

Here i want to return a string "results" to some other method like this

string data= obj1.GetEmp()

How can i achieve this.. Any help would be appriciated.. Thanks

Easiest way to do this is rewrite method using async , like this:

public async Task<string> GetEmpAsync()
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri("http://sdw2629/empservice/EmployeeInfo.svc/Employee"));
    request.Method = "GET";
    request.ContentType = "application/json; charset=utf-8";
    var response = await request.GetResponseAsync();
    using (StreamReader httpwebStreamReader = new StreamReader(response.GetResponseStream()))
    {
        string results = await httpwebStreamReader.ReadToEndAsync();
        //execute UI stuff on UI thread.
        return results;
    }

}

Then you can get result with code like this:

var results = await GetEmpAsync();

If you are using older version and don't have async , you can do it in blocking way to get result:

public string GetEmpBlocking()
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri("http://sdw2629/empservice/EmployeeInfo.svc/Employee"));
    request.Method = "GET";
    request.ContentType = "application/json; charset=utf-8";
    var response = request.GetResponse();
    using (StreamReader httpwebStreamReader = new StreamReader(response.GetResponseStream()))
    {
        string results = httpwebStreamReader.ReadToEnd();
        //execute UI stuff on UI thread.
        return results;
    }
}

And get result like this:

var results = GetResultBlocking();

Р.S. you can also consider using Microsoft.Bcl.Async to support async in earlier versions.

Just use the

var resp = request.GetResponse();

instead of the

request.BeginGetResponse(...);

With the synchrounous GetResonse, you can return the string directly.

public string GetEmp()
{
    var request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri(@"http://.../data.json"));
    request.Method = "GET";
    request.ContentType = "application/json; charset=utf-8";

    var resp = request.GetResponse();

    using (var httpWebStreamReader = new StreamReader(resp.GetResponseStream()))
    {
        var result = httpWebStreamReader.ReadToEnd();
        return result;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM