Friday, March 6, 2015
Fibonacci Async Lab
using System.Windows.Forms;
using System.Threading; //Added
namespace ZauBawk_FibonacciAsyncLab_Due_3_4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void btnRunFibonacciWithAsync_Click(object sender, EventArgs e)
{
long userInput;
long.TryParse(txtValue.Text, out userInput);
double result = await GetSumOfRandomValuesAsync(userInput);
richTextBox1.AppendText("Fibonacci(" + userInput + ") = " + result + "\n");
}
private async Task<long> GetSumOfRandomValuesAsync(long inputValue)
{
long result = 0;
Task<long> task = Task<long>.Factory.StartNew(() =>
{
result = Fibonacci(inputValue);
return result;
});
await task;
return task.Result; //calling result from a task
}
//method for lab
private long Fibonacci(long n)
{
if (n == 0) return 0;
if (n == 1) return 1;
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
private void btnTestIftheUIisLocked_Click(object sender, EventArgs e)
{
MessageBox.Show("Testing if the UI is locked.");
}
}
}
//Lab Assingment due Wed 3/4
//New project
//given the fibonacci series method
//define an asynchronous method that takes an input n and returns its Fibonnacci value
//Call this method (from a button click) and display its return value.
//The UI thread should not lock up.
Labels:
C#Advance
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment