omginput
omginput

Reputation: 69

Get Value from async Method?

I want to have the returned value from the send Operation which is a string and use it in the public MainPage() section. I tried this way, bot doesn´t work. Any idea how to get this value out of the send() Method?

public sealed partial class MainPage : Page
{

    public MainPage()
    {
        this.InitializeComponent();

        string stringData = "";

        stringData = "aktion=getBenutzer&name=" + Login.getBenutzername();
        //send(stringData);

        //textBlock1.Text = getContentOfSendOperation();



        button.Foreground = Einstellungen.getBrush();
        button1.Foreground = Einstellungen.getBrush();
        button2.Foreground = Einstellungen.getBrush();


        stringData = "aktion=getMitarbeiterListe";

        //string mitarbeiterListe = getContentOfSendOperation();
        var task = send(stringData);
        string mitarbeiterListe = task.Result;
        textBlock1.Text = mitarbeiterListe;
        //comboBox.Items.Add()



    }



    public Frame globalFrame { get { return _mainFrame; } }



    private void button_Click(object sender, RoutedEventArgs e)
    {
        _mainFrame.Navigate(typeof(Datenbank));
    }

    public async Task<String> send(string stringData)
    {
        System.Net.Http.HttpClient oHttpClient = new System.Net.Http.HttpClient();

        Uri uri = new Uri("*********");
        oHttpClient.DefaultRequestHeaders.UserAgent.ParseAdd("moralsKite/DesktopTestClient");

        var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, uri);
        request.Content = new StringContent(stringData, Encoding.UTF8, "application/x-www-form-urlencoded");
        var reponse = await oHttpClient.SendAsync(request);

        if (reponse.IsSuccessStatusCode)
        {
            return "??";
            //return await reponse.Content.ReadAsStringAsync();



        }
        return "!!";
    }

    private void button1_Click_1(object sender, RoutedEventArgs e)
    {
        _mainFrame.Navigate(typeof(Einstellungen));
    }

    private void button2_Click_1(object sender, RoutedEventArgs e)
    {
        _mainFrame.Navigate(typeof(Ueber));
    }
}


}

Upvotes: 0

Views: 1351

Answers (2)

Romasz
Romasz

Reputation: 29792

You cannot run asynchronous operation and await it in constructor. In your example the task can run little longer (varying on signal and so on), the constructor of a class should be fast. Better subscribe to one of the page's events like Loaded and put your work there:

public MainPage()
{
    this.InitializeComponent();
    // rest of code
    this.Loaded += MainPage_Loaded;
}

private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    // Following Peter Torr's comment - Loaded event can be fired multiple times 
    // for example once you navigate back to the page
    this.Loaded -= MainPage_Loaded; // deregister from event if you want to run it once
    stringData = "aktion=getMitarbeiterListe";
    // in your send method uncomment the line:
    // return await reponse.Content.ReadAsStringAsync();
    // then it will return asynchronously the content as string and can be used like this:
    string mitarbeiterListe = await send(stringData);
}

Events can be async then there shouldn't be problems, you may also implement an information for user that something is loading in the background.

Upvotes: 1

Maxim Fleitling
Maxim Fleitling

Reputation: 562

Instead of :

var task = send(stringData);
        string mitarbeiterListe = task.Result;

use

string mitarbeiterListe = await send(stringData);

Make the method where you call send data async. I do not recommend to call gathering of data in constructor.

Upvotes: 0

Related Questions