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(); } }
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.
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.
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.
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.
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.
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.
Your button_Clickmethod needs to be async. Place a asyncbetween privateand void. private async void button_Click(object sender, EventArgs e) { await LongOperation(); }
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With