Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use async to increase WinForms performance?

i was doing some processor heavy task and every time i start executing that command my winform freezes than i cant even move it around until the task is completed. i used the same procedure from microsoft but nothing seem to be changed.

my working environment is visual studio 2012 with .net 4.5

private async void button2_Click(object sender, EventArgs e) {     Task<string> task = OCRengine();               rtTextArea.Text = await task; }  private async Task<string> OCRengine() {     using (TesseractEngine tess = new TesseractEngine(            "tessdata", "dic", EngineMode.TesseractOnly))     {         Page p = tess.Process(Pix.LoadFromFile(files[0]));         return p.GetText();     } } 
like image 977
Serak Shiferaw Avatar asked Feb 19 '13 16:02

Serak Shiferaw


People also ask

Can an async method run on the UI thread of a Windows Forms app?

You can start an async operation from the UI thread, await it without blocking the UI thread, and naturally resume on the UI thread when it's done.

What is the advantage of async await in C#?

The biggest advantage of using async and await is, it is very simple and the asynchronous method looks very similar to a normal synchronous methods. It does not change programming structure like the old models (APM and EAP) and the resultant asynchronous method look similar to synchronous methods.

What's the point of async await?

Note: The purpose of async / await is to simplify the syntax necessary to consume promise-based APIs. The behavior of async / await is similar to combining generators and promises. Async functions always return a promise.

Can I use async without await C#?

C# Language Async-Await Returning a Task without awaitMethods that perform asynchronous operations don't need to use await if: There is only one asynchronous call inside the method. The asynchronous call is at the end of the method. Catching/handling exception that may happen within the Task is not necessary.

How can I improve the performance of my asynchronous code?

But you may also try to make your async operations coarser grained. This can improve performance, simplify debugging and overall make your code easier to reason. Not every small piece of code has to be asynchronous.

Do async methods that await a non-completed task affect performance?

A performance overhead of async methods that await non-completed task is way more substantial (~300 bytes per operation on x64 platform). And, as always, measure first. If you see that an async operation causes a performance problem, you may switch from Task<T> to ValueTask<T>, cache a task or make a common execution path synchronous if possible.

Is it possible to use async in the button_click method?

Your button_Clickmethod needs to be async. Place a asyncbetween privateand void. private async void button_Click(object sender, EventArgs e) { await LongOperation(); }

What is the performance overhead of async in Java?

If the async method completes synchronously the performance overhead is fairly small. If the async method completes synchronously the following memory overhead will occur: for async Task methods there is no overhead, for async Task<T> methods the overhead is 88 bytes per operation (on x64 platform).


1 Answers

Yes, you're still doing all the work on the UI thread. Using async isn't going to automatically offload the work onto different threads. You could do this though:

private async void button2_Click(object sender, EventArgs e) {     string file = files[0];     Task<string> task = Task.Run(() => ProcessFile(file));            rtTextArea.Text = await task; }  private string ProcessFile(string file) {     using (TesseractEngine tess = new TesseractEngine("tessdata", "dic",                                                        EngineMode.TesseractOnly))     {         Page p = tess.Process(Pix.LoadFromFile(file));         return p.GetText();     } } 

The use of Task.Run will mean that ProcessFile (the heavy piece of work) is executed on a different thread.

like image 194
Jon Skeet Avatar answered Oct 06 '22 01:10

Jon Skeet