Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# async in wpf

private async void ButtonClick(object sender, RoutedEventArgs e)
{
    // modify UI object in UI thread
    txt.Text = "started";

    // run a method in another thread
    await HeavyMethod(txt);
    // <<method execution is finished here>>

    // modify UI object in UI thread
    txt.Text = "done";
}

// This is a thread-safe method. You can run it in any thread
internal async Task HeavyMethod(TextBox textBox)
{
    while (stillWorking)
    {
        textBox.Dispatcher.Invoke(() =>
        {
            // UI operation goes inside of Invoke
            textBox.Text += ".";
            // Note that: 
            //    Dispatcher.Invoke() blocks the UI thread anyway
            //    but without it you can't modify UI objects from another thread
        });
        
        // CPU-bound or I/O-bound operation goes outside of Invoke
        // await won't block UI thread, unless it's run in a synchronous context
        await Task.Delay(51);
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: belgiumcampus 
Csharp :: google tradutor 
Csharp :: c# filesystemwatcher 
Csharp :: how to check if a number is prime or not c# 
Csharp :: c# resize image from byte array 
Csharp :: c# window instantly close 
Csharp :: c# array inst working 
Csharp :: c# jump space 
Csharp :: embed video to exe file with c# 
Csharp :: c# .net stringify data query 
Csharp :: convert foreach to linq c# 
Html :: lodash cdn 
Html :: calling javascript file in html 
Html :: htmjl favicons 
Html :: only accept image file input 
Html :: html input price 
Html :: #ubuntu "demarrer vcs en super user" 
Html :: how to tab in html 
Html :: target blanc 
Html :: add favicon to html 
Html :: Add a viewport meta tag to the document head to set the width of the layout viewport equal to the width of the device and set the initial scale of the viewport to 1.0. 
Html :: non breaking space html 
Html :: link to call a phone number 
Html :: bootstrap 5 align middle 
Html :: abrir pdf con html 
Html :: html textarea 
Html :: open new tab when clicking link html 
Html :: how to give color to text in html 
Html :: html link para descargar archivo de audio 
Html :: text color html 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =