Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Folgender Code ist mit dem neuen Async Framework geschrieben.

Kompiliert wunderbar.

Erwartung:

Meine Anwendung besteht aus einer Textbox und einem Button, welcher nach "onclick" die Webseite Googles lädt und das Result in der Textbox darstellt.

Das geschieht auch.

Aber - und da liegt der Hase im Pfeffer:

geschieht der Aufruf wider erwarten nicht asynchron/nonblocking, sondern die GUI hängt und wartet brav, bis Google geantwortet hat.


namespace AsyncTest
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

public async Task GetGoogle()
{
WebClient webclient = new WebClient();
textBox1.Text=await webclient.DownloadStringTaskAsync(new Uri("http://www.google.de"));

}

async void WaitForTextAndFillTextbox()
{
await GetGoogle();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
WaitForTextAndFillTextbox();
}
}
}
[/php]

Hat jemand eine Idee, warum es nicht so funktioniert, wie erwartet?

Irgendwie stehe ich auf dem Schlauch.

Danke :]

hmm ich hab mich damit noch nicht auseinander gesetzt, sieht auf jeden Fall sehr interessant aus.

Aber vielleicht hat der da irgendwie ein Problem weil du das direkt der TextBox zuweist und klappt dann so:


string text = await webclient.DownloadStringTaskAsync(new Uri("http://www.google.de"));
textBox1.Text=text;
[/PHP]

Ist aber nur ein Schuss ins Blaue und wirklich vorstellen kann ichs mir auch nicht...

  • Autor


namespace AsyncTest
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
WebClient webclient = new WebClient();
public MainWindow()
{
InitializeComponent();
}

private async void button1_Click(object sender, RoutedEventArgs e)
{
string result = await webclient.DownloadStringTaskAsync(textBox2.Text);
textBox1.Text = result;
}
}
}
[/php]

Ich habe es auf das folgende runtergebrochen.

TextBox2 dient quasi als Addressleiste und Textbox1 als Ausgabe.

Starte ich den Aufruf zum ersten mal, blockiert der Aufruf (noch);

ab da an habe ich das Gefühl, dass das Ganze nonblocking läuft ... sehr komisch.

Das kommt drauf an wie der Compiler die Zeile intern genau umbaut damit das plötzlich ansynchron ist.

Eben. Da liegt nämlich der Clou an der ganzen Geschichte:

Es handelt sich um Syntactic Sugar, der einem das ganze Backgroundworker/Thread starten wasauchimmer erleichtern soll.

Lazy Loading trifft es nicht.

Eduasync part 6: using the infrastructure manually - Jon Skeet: Coding Blog <- liefert einen Hinweis, wie der Compiler vorgehen könnte - quasi "re-engineering" (statt "reverse engineering").

Aber merkwürdig ist das Verhalten meiner Anwendung schon.

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.