I have a question about async\await in a C# .NET app. I'm actually trying to solve this problem in a Kinect based application but to help me illustrate, I've crafted this analogous example:
Imagine that we have a Timer, called timer1 which has a Timer1_Tick event set up. Now, the only action I take on that event is to update the UI with the current date time.
private void Timer1_Tick(object sender, EventArgs e)
{
txtTimerValue.Text = DateTime.Now.ToString("hh:mm:ss.FFF", CultureInfo.InvariantCulture);
}
This is simple enough, my UI updates every few hundredths of seconds and I can happily watch time go by.
Now imagine that I also want to also calculate the first 500 prime numbers in the same method like so:
private void Timer1_Tick(object sender, EventArgs e)
{
txtTimerValue.Text = DateTime.Now.ToString("hh:mm:ss.FFF", CultureInfo.InvariantCulture);
List<int> primeNumbersList = WorkOutFirstNPrimeNumbers(500);
PrintPrimeNumbersToScreen(primeNumbersList);
}
private List<int> WorkOutFirstNPrimeNumbers(int n)
{
List<int> primeNumbersList = new List<int>();
txtPrimeAnswers.Clear();
int counter = 1;
while (primeNumbersList.Count < n)
{
if (DetermineIfPrime(counter))
{
primeNumbersList.Add(counter);
}
counter++;
}
return primeNumbersList;
}
private bool DetermineIfPrime(int n)
{
for (int i = 2; i < n; i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
private void PrintPrimeNumbersToScreen(List<int> primeNumbersList)
{
foreach (int primeNumber in primeNumbersList)
{
txtPrimeAnswers.Text += String.Format("The value {0} is prime \r\n", primeNumber);
}
}
This is when I experience the problem. The intensive method that calculates the prime numbers blocks the event handler from being run - hence my timer text box now only updates every 30 seconds or so.
My question is, how can I resolve this while observing the following rules:
- I need my UI timer textbox to be as smooth as it was before, probably by pushing the intensive prime number calculation to a different thread. I guess, this would enable the event handler to run as frequently as before because the blocking statement is no longer there.
- Each time the prime number calculation function finishes, it's result to be written to the screen (using my PrintPrimeNumbersToScreen() function) and it should be immediately started again, just in case those prime numbers change of course.
I have tried to do some things with async/await and making my prime number calculation function return a Task> but haven't managed to resolve my problem. The await call in the Timer1_Tick event still seems to block, preventing further execution of the handler.
Any help would be gladly appreciated - I'm very good at accepting correct answers :)
Update: I am very grateful to @sstan who was able to provide a neat solution to this problem. However, I'm having trouble applying this to my real Kinect-based situation. As I am a little concerned about making this question too specific, I have posted the follow up as a new question here: Kinect Frame Arrived Asynchronous
May not be the best solution, but it will work. You can create 2 separate timers. Your first timer's
Tick
event handler only needs to deal with yourtxtTimerValue
textbox. It can remain the way you had it originally:For your 2nd timer's
Tick
event handler, define theTick
event handler like this:You'll notice that I changed your
PrintPrimeNumbersToScreen()
method toConvertPrimeNumbersToString()
(the rest remains the same). The reason for the change is that you really want to minimize the amount of work being done on the UI thread. So best to prepare the string from the background thread, and then just do a simple assignment to thetxtPrimeAnswers
textbox on the UI thread.EDIT: Another alternative that can be used with a single timer
Here is another idea, but with a single timer. The idea here is that your
Tick
even handler will keep executing regularly and update your timer value textbox every time. But, if the prime number calculation is already happening in the background, the event handler will just skip that part. Otherwise, it will start the prime number calculation and will update the textbox when it's done.So you want to start a Task without waiting for the result. When the task has finished calculating it should update the UI.
First some things about async-await, later your answer
The reason that your UI isn't responsive during the long action is because you didn't declare your event handler async. The easiest way to see the result of this is by creating an event handler for a button:
synchronous - UI is blocked during execution:
asynchronous - UI is responsive during execution:
Note the differences:
Note:
<TResult
> instead of TResult.<TResult
> is a TResult.<TResult
>.Your problem: timer tick reported when calculations still busy
The problem is that your timer is faster than your calculations. What do you want if a new tick is reported when the previous calculations are not finished
(1) Start the Task, but do not await for it.
(2) ignore the tick if the task is still busy:
(3) Start a task that calculates continuously
You should avoid using async/await (despite how good they are) because Microsoft's Reactive Framework (Rx) - NuGet either "Rx-WinForms" or "Rx-WPF" - is a far better approach.
This is the code you would need for a Windows Forms solution:
That's it. Very simple. It all happens on background threads before being marshalled back to the UI.
The above should be fairly self explanatory, but yell out if you need any further explanation.