There are plenty of ansyc samples over the internet, and most of them are different and not satisfy your requirements. Actually, async pattern depends of its creator, and can be implement on various ways. It is important to understand the async pattern in order to use it. Only on this way, you can stop searching for exact sample you need, and start coding your own async code. More that year ago, I wrote simple blog post about async pattern (part 1 and part 2) (Bosnian language), and also wrote how to call Entity Framework with async pattern as well.
Today I am going to show you how old synchronous code block convert in to asynchronous. I think it is interesting because async pattern can improve your existing applications on various ways. First of all async pattern can increase responsiveness of an application, performance, etc.
First of all, create simple Windows Forms sample and implement synchronous code.This will represent our old application, in which we are going to implement new programming paradigm.
1. Create Windows Forms Project, Name it “WinFormsAsyncSample”
2. Design your main form (Form1.cs) exactly as picture shows below, and implement events.
As picture shows we have few labels, one text box, one progress bars, and two buttons.
Note: At the end of the blog you can download both versions (nonasync and async) of this sample.
The sample application calculates how many prime numbers exist in range. You need to enter number, press run button. The program starts counting. The progress bars informs user status of counting.
Lets see the implementation of runBtn_Click event:
private void btnRun_Click(object sender, EventArgs e) { if(!int.TryParse(textBox1.Text,out m_number)) m_number= 1000000; textBox1.Text = m_number.ToString(); progressBar1.Maximum = m_number; progressBar1.Minimum = 0; progressBar1.Value = 0; //call start calculation startCounting(); }
At the beginning of the btnRun_Click we prepare progressBar, and also convert text from textbox in to int type.At the end of the function startConting method is called. Here is the source code of the method:
private void startCounting() { int counter=0; for(int i=2;i<m_number; i++) { bool retVal= IsPrime(i); progressBar1.Value++; if (retVal) { counter++; label3.Text = "Result is: " + counter.ToString(); } } }
The method is very simple. It iterates from 2 to specified number by calling helper method IsPrime (see source code of the blog post) to check if certain number is prime. Then the method increased the counter variable and tried to update label about current count value. If you run the sample and press run button, you can see that the application is not responsive on user input, and represent classic sequential, synchronous single thread application.
Now I am going to show how this implementation can be converted in to async with minimum code changes.We are going to change only startCounting method, other code will remain the same. Explanation is divided in only 3 steps, which is enough to convert our code in to full async pattern.
- Put async keyword right after public modifier of startCounting method.
Explanation: Every method which implements async patter needs to be decorated with async.
- Put sequential implementation in to Task action, and wait.
Explanation: With this, you define a Task object which will run the code without blocking main thread. The simplest implementation is the following:
private async void startCalculation() { var task = new Task(() => { int counter = 0; for (int i = 2; i < m_number; i++) { bool retVal = IsPrime(i); this.Invoke((Action)(()=> { progressBar1.Value++; if (retVal) { counter++; label3.Text = "Result is: " + counter.ToString(); } })); } }); task.Start(); await task; }
Now if you run the sample, you have fully asynchronous implementation. Main thread is free and can receive user input. So this is the simplest way how to convert your sync code in to async. This is the case when Task object create another thread in order to execute the code. That’s why we called this.Invoke method in order to set controls properties progressBar.Value and label3.Text. Everything else remain the same as in previous implementation.
- Create global variable of type CancellationTokenSource and call Cancel method from Cancel Event handler.
Explanation: On this way we can cancel counting at any time.With this case we need to implements extra code in our previous implementation like the following.
private async void RunProces() { if (m_IsRunning) return; m_IsRunning = true; int counter=0; if (m_ct != null) { m_ct.Dispose(); m_ct = null; } m_ct = new CancellationTokenSource(); var task = new Task(() => { for (int i = 0; i < m_number; i++) { bool retVal = IsPrime(i); this.Invoke((Action)(()=> { progressBar1.Value++; if (retVal) { counter++; label3.Text = "Result is: " + counter.ToString(); } })); if (m_ct.IsCancellationRequested) { m_IsRunning = false; return; } } }, m_ct.Token); task.Start(); await task; }
With the last code implementation you have full of async patter in yur old application.
In this blog post I have tried to explain as simple as possible the way how you can convert you old sync code in to new async programming pattern.
Source code sample used in this blog post:
On this link you can find sequential version of the sample.
On this link you can find the final solution of async pattern.
Moncler Outlet Online Store. Cheap Moncler Jackets, Moncler Coats and Moncler Vests
prada handbags http://www.alexcd.com/
Shop Cheap Prada Handbags and Prada Purses in Prada Outlet online store.Choose from a large selection of 100% Authentic Prada Bags on sale.
michael kors factory outlet http://www.billcost.gr
Everyone loves what you gjys tend to be up too. This sort
of clever work aand coverage! Keep up the very good works guys I’ve incorporated
you guys to our blogroll.