- Joined
- Aug 30, 2007
- Messages
- 269
- Reaction score
- 0
- Points
- 16
So I need help on my homework. I am not sure what they are asking me to do or how to do it. The question is
Modify the example of fig 23.3 to process the results by using the array returned by the task produced by task method WhenAll
where is the array being returned ? Do I need to assign it to a new array ?
My teacher wrote this with the task also
Clarification:
The original code (fig 23.3) calls WhenAll to wait, then just uses the task1 and task2 objects to determine start/stop times. This exercise is asking you to get the array returned by WhenAll and use the data in that array to calculate those times. There is some discussion about this in section 23.5.1. It is not really necessary in this case since there are only two tasks--but the idea is that the array could potentially hold a lot of tasks, so you could examine ("process") the times for all of them. Your output will basically be the same after your modification as it was before you made the change.
namespace FibonacciAsynchronous
{
public partial class AsynchronousTestForm : Form
{
public AsynchronousTestForm()
{
InitializeComponent();
}
// start asynchronous calls to Fibonacci
private async void startButton_Click(object sender, EventArgs e)
{
outputTextBox.Text =
"Starting Task to calculate Fibonacci(46)\r\n";
// create Task to perform Fibonacci(46) calculation in a thread
Task<TimeData> task1 = Task.Run(() => StartFibonacci(46));
outputTextBox.AppendText(
"Starting Task to calculate Fibonacci(45)\r\n");
// create Task to perform Fibonacci(45) calculation in a thread
Task<TimeData> task2 = Task.Run(() => StartFibonacci(45));
await Task.WhenAll(task1, task2); // wait for both to complete
// determine time that first thread started
DateTime startTime =
(task1.Result.StartTime < task2.Result.StartTime) ?
task1.Result.StartTime : task2.Result.StartTime;
// determine time that last thread ended
DateTime endTime =
(task1.Result.EndTime > task2.Result.EndTime) ?
task1.Result.EndTime : task2.Result.EndTime;
// display total time for calculations
double totalMinutes = (endTime - startTime).TotalMinutes;
outputTextBox.AppendText(
$"Total calculation time = {totalMinutes:F6} minutes\r\n");
}
// starts a call to fibonacci and captures start/end times
TimeData StartFibonacci(int n)
{
// create a TimeData object to store start/end times
var result = new TimeData();
AppendText($"Calculating Fibonacci({n})");
result.StartTime = DateTime.Now;
long fibonacciValue = Fibonacci;
result.EndTime = DateTime.Now;
AppendText($"Fibonacci({n}) = {fibonacciValue}");
double minutes =
(result.EndTime - result.StartTime).TotalMinutes;
AppendText($"Calculation time = {minutes:F6} minutes\r\n");
return result;
}
// Recursively calculates Fibonacci numbers
public long Fibonacci(long n)
{
if (n == 0 || n == 1)
{
return n;
}
else
{
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}
// append text to outputTextBox in UI thread
public void AppendText(String text)
{
if (InvokeRequired) // not GUI thread, so add to GUI thread
{
Invoke(new MethodInvoker(() => AppendText(text)));
}
else // GUI thread so append text
{
outputTextBox.AppendText(text + "\r\n");
}
}
}
}
Modify the example of fig 23.3 to process the results by using the array returned by the task produced by task method WhenAll
where is the array being returned ? Do I need to assign it to a new array ?
My teacher wrote this with the task also
Clarification:
The original code (fig 23.3) calls WhenAll to wait, then just uses the task1 and task2 objects to determine start/stop times. This exercise is asking you to get the array returned by WhenAll and use the data in that array to calculate those times. There is some discussion about this in section 23.5.1. It is not really necessary in this case since there are only two tasks--but the idea is that the array could potentially hold a lot of tasks, so you could examine ("process") the times for all of them. Your output will basically be the same after your modification as it was before you made the change.
namespace FibonacciAsynchronous
{
public partial class AsynchronousTestForm : Form
{
public AsynchronousTestForm()
{
InitializeComponent();
}
// start asynchronous calls to Fibonacci
private async void startButton_Click(object sender, EventArgs e)
{
outputTextBox.Text =
"Starting Task to calculate Fibonacci(46)\r\n";
// create Task to perform Fibonacci(46) calculation in a thread
Task<TimeData> task1 = Task.Run(() => StartFibonacci(46));
outputTextBox.AppendText(
"Starting Task to calculate Fibonacci(45)\r\n");
// create Task to perform Fibonacci(45) calculation in a thread
Task<TimeData> task2 = Task.Run(() => StartFibonacci(45));
await Task.WhenAll(task1, task2); // wait for both to complete
// determine time that first thread started
DateTime startTime =
(task1.Result.StartTime < task2.Result.StartTime) ?
task1.Result.StartTime : task2.Result.StartTime;
// determine time that last thread ended
DateTime endTime =
(task1.Result.EndTime > task2.Result.EndTime) ?
task1.Result.EndTime : task2.Result.EndTime;
// display total time for calculations
double totalMinutes = (endTime - startTime).TotalMinutes;
outputTextBox.AppendText(
$"Total calculation time = {totalMinutes:F6} minutes\r\n");
}
// starts a call to fibonacci and captures start/end times
TimeData StartFibonacci(int n)
{
// create a TimeData object to store start/end times
var result = new TimeData();
AppendText($"Calculating Fibonacci({n})");
result.StartTime = DateTime.Now;
long fibonacciValue = Fibonacci;
result.EndTime = DateTime.Now;
AppendText($"Fibonacci({n}) = {fibonacciValue}");
double minutes =
(result.EndTime - result.StartTime).TotalMinutes;
AppendText($"Calculation time = {minutes:F6} minutes\r\n");
return result;
}
// Recursively calculates Fibonacci numbers
public long Fibonacci(long n)
{
if (n == 0 || n == 1)
{
return n;
}
else
{
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}
// append text to outputTextBox in UI thread
public void AppendText(String text)
{
if (InvokeRequired) // not GUI thread, so add to GUI thread
{
Invoke(new MethodInvoker(() => AppendText(text)));
}
else // GUI thread so append text
{
outputTextBox.AppendText(text + "\r\n");
}
}
}
}